0

I need some help with pointers, mostly. I've malloc'd a BUFFER pointer to the struct data type RGBTRIPLE. Though I can't access its properties.


// Reflect image horizontally
void reflect(int height, int width, RGBTRIPLE image[height][width])
{
    // if the row is even width
    if (width % 2 == 0)
    {
        for (int i = 0; i < width; i++)
        {
            for (int j = 0; j < height; j++)
            {
                // starting from first and last of row all the way to middle
                while (i < width-i)
                {
                    // initialize buffer to first
                    RGBTRIPLE *BUFFER;
                    BUFFER = malloc(sizeof(RGBTRIPLE));
                    *BUFFER = image[i][j];

                    // Make first the last
                    int offset = width - i + 1;
                    image[i][j].rgbtBlue = image[offset][j].rgbtBlue;
                    image[i][j].rgbtGreen = image[offset][j].rgbtGreen;
                    image[i][j].rgbtRed = image[offset][j].rgbtRed;

                    //replace last with buffer
                    image[offset][j].rgbtBlue = *BUFFER.rgbtBlue;
                    image[offset][j].rgbtGreen = *BUFFER.rgbtGreen;
                    image[offset][j].rgbtRed = *BUFFER.rgbtRed;
                }
            }
            // free(BUFFER);
        }
    }
    return;
}

Here are some errors I'm receiving:

helpers.c:103:56: error: member reference type 'RGBTRIPLE *' is a pointer; did you mean to use '->'?
                    image[offset][j].rgbtBlue = *BUFFER.rgbtBlue;
                                                 ~~~~~~^
                                                       ->
helpers.c:103:49: error: indirection requires pointer operand ('int' invalid)
                    image[offset][j].rgbtBlue = *BUFFER.rgbtBlue;
                                                ^~~~~~~~~~~~~~~~
helpers.c:104:57: error: member reference type 'RGBTRIPLE *' is a pointer; did you mean to use '->'?
                    image[offset][j].rgbtGreen = *BUFFER.rgbtGreen;
                                                  ~~~~~~^
                                                        ->
helpers.c:104:50: error: indirection requires pointer operand ('int' invalid)
                    image[offset][j].rgbtGreen = *BUFFER.rgbtGreen;
                                                 ^~~~~~~~~~~~~~~~~
helpers.c:105:55: error: member reference type 'RGBTRIPLE *' is a pointer; did you mean to use '->'?
                    image[offset][j].rgbtRed = *BUFFER.rgbtRed;
                                                ~~~~~~^
                                                      ->
helpers.c:105:48: error: indirection requires pointer operand ('int' invalid)
                    image[offset][j].rgbtRed = *BUFFER.rgbtRed;
1

As noted by the compiler, when you have a pointer to a struct and you want to access one of its members, you use the arrow operator:

BUFFER->rgbtRed

You can use the classic dereferencing operator (*), but because of operator precedence you have to use parentheses to dereference the pointer before accessing a struct member, which gets a little clumsy:

(*BUFFER).rgbtRed

The arrow operator, as such, is "syntactic sugar".

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .