Shivam
Shivam

Reputation: 31

C pointer error -> expression must be a modifiable lvalue

I am not able to set a data member of a struct object by manipulating the data members of another struct object and getting an error : expression must be a modifiable lvalue.

I have already seen all the answers to this particular error code, but couldn't get any satisfactory result.

IMAGE STRUCT IS DEFINED AS FOLLOWS:

typedef struct{
    int w,h,c;            
    float *data;   
}image;

This image struct contains the data as a pointer to a float value that contains the pixel values of a particular image. Now the task is to change all the pixels to grayscale. We have a criteria denoted as Y' = 0.299 R' + 0.587 G' + .114 B'.Or for every pixel value in all the 3 channels, I have to take this weighted mean.

MY FUNCTION TO CHANGE RGB VALUES TO GRAYSCALE IS THIS:

image rgb_to_grayscale(image im)
{
    assert(im.c == 3);
    image gray = make_image(im.w, im.h, 1);        // Creates a image struct with *data containing 
                                                   // 1 Channel

    // Creating pointers to struct
    image * ptr_gray , * ptr_im;

    // Point to the address of the struct objects
    // Gray Image
    ptr_gray = &gray; 
    float * gray_data = ptr_gray->data;
    float(*gray_data_matrix) [im.w][im.h] = gray_data;

    // Point to the address of the struct objects
    // Given Image
    ptr_im = &im; 
    float * im_data = ptr_im->data;
    float(*im_data_matrix) [im.w][im.h] = im_data;

    // Now we have the weighted mean like:
    //Y' = 0.299 R' + 0.587 G' + .114 B'

    // So for a given *data I have to calculate the value over the whole 3-D Matrix

    float red_matrix[im.h][im.w];
    float green_matrix[im.h][im.w];
    float blue_matrix[im.h][im.w];

    int k =0;
    for(int i= 0; i< im.h ; i++){
        for(int j = 0; j < im.w ; j++){
            while(k<im.c)){
                if(k == 0){
                    red_matrix[i][j] = *(*(*(im_data_matrix +k) + i) + j)*0.299 ;
                }
                if(k == 1){
                    green_matrix[i][j] =   *(*(*(im_data_matrix +k) + i) + j)*0.587;
                }
                if(k == 2){
                    blue_matrix[i][j] = *(*(*(im_data_matrix +k) + i) + j)*0.114;
                }

            }
            *(*(gray_data_matrix+i)+j) = *(*(red_matrix + i)+j) + 
                                            *(*(green_matrix + i)+j) + 
                                                *(*(blue_matrix + i)+j);
    k++;
        }

    }

    return gray;
}

This line throws error ::

*(*(gray_data_matrix+j)+i) = *(*(red_matrix + i)+j) + 
                                            *(*(green_matrix + i)+j) + 
                                                *(*(blue_matrix + i)+j);

The error is : expression must be a modifiable lvalue. How to resolve this error??

Upvotes: 1

Views: 350

Answers (1)

Guillaume Petitjean
Guillaume Petitjean

Reputation: 2718

For me, the problem is in the data structures you are using. In particular the definition of the variable gray_data_matrix in float(*gray_data_matrix) [im.w][im.h] = gray_data;. It doesn't compile with standard gcc and I don't understand what you are trying to define here.

You could simply travel the RGB image im.data pixels and at each iteration proceed to your weighted average computation.

Something like this (I didn't test !):

for(int i= 0; i< im.h ; i++){
        for(int j = 0; j < im.w ; j++){
               gray.data[i][j] =   im.data[0][i][j]*0.299 
                                 + im.data[1][i][j]* xxx
                                 + im.data[2][i][j]* xxx;
        }
}

Upvotes: 1

Related Questions