Thalia
Thalia

Reputation: 14615

c++ Passing a const struct that contains a pointer - how to move through the values?

    typedef struct {
        unsigned int width;
        unsigned int height;
        PBYTE pixels;
    } PBYTEImage;

    void someFunction(const PBYTEImage& inputImage)
    {
        for (unsigned int x = 0; x < inputImage.width*inputImage.height; x++) 
        {
            unsigned char y = *(inputImage.pixels);
->          inputImage.pixels += 1;
        }
    }

Error: expression must be a modifiable value

Am I modifying the contents of PBYTE ? I thought inputImage.pixels += 1; would only advance the pointer PBYTE to the next value... Is that correct ?

What want:

I don't think I can make the items inside struct as const since the caller has to assign their values.

What is the best way to do it ?

    void someFunction(const PBYTEImage* inputImage)

had the same effect.. it seems the const applies to the struct which does not allow me to modify its pointer, including the value the pointer is pointing at...

Do I have to drop the const ? Is there no way to show that the function does not modify the data ?

Edit: the PBYTE is

typedef unsigned char BYTE;   
typedef BYTE near *PBYTE;

defined in minwindef.h or windef.h

Sorry for not mentioning it

Upvotes: 1

Views: 661

Answers (2)

First, you have a typo in the exit condition of your for loop: you have written

inputImage*height

but it should be

inputImage.height

Apart from this, you haven't told us what PBYTE is, but from this line

unsigned char y = *(inputImage.pixels);

I suppose that somewhere you have

typedef unsigned char* PBYTE;

So PBYTE is a pointer. The expression

inputImage.pixels += 1;

does indeed try to increment the pointer, pixels. Since pixels is a member of your struct, and your struct is passed as a const, you are getting the error. So yes, if you want to keep your struct as it is and increase the pointer, you must drop the const in your function definition. Changing from passing inputImage as reference to passing it as pointer won't help either.

Do I have to drop the const ? Is there no way to show that the function does not modify the data ?

Maybe you are expecting to be safe from a const-ness point of view because you are just moving a pointer and not touching the data that it is pointing to: that's right, but in this case the pointer itself is your data, and you are trying to change it. So your function does modify the data.

Upvotes: 0

Axalo
Axalo

Reputation: 2953

Do I have to drop the const ? Is there no way to show that the function does not modify the data ?

Actually, your function does change the parameters data since you're incrementing inputImage.pixels. So you probably want to store inputImage.pixels and iterate that instead.

void someFunction(const PBYTEImage& inputImage)
{
    PBYTE pPixels = inputImage.pixels;
    for (unsigned int x = 0; x < inputImage.width*inputImage*height;) 
    {
        unsigned char y = *pPixels;
        pPixels += 1;
    }
}

Upvotes: 2

Related Questions