c++pointersoperator-keywordoperator-precedence

C++ and operator precedence


I know this long string could be made easier to read, but I don't want that!

I want to get the color of a pixel and I'm using SDL. Although this is not very relevant to the question...

http://www.gamedev.net/topic/502040-sdl-get-pixel-color/

http://www.libsdl.org/docs/html/sdlsurface.html

Shows that to get this color value, you do:

Uint32 *pixels = (Uint32 *)surface->pixels;
return pixels[number];

Well I don't have it just like that and I also wanted to try and grasp the whole operator precedence stuff..

I've tried a bit but I can't get it to work with the last [] operator.

So... I got this:

vector<Class*>* pointer_To_A_Vector_With_Pointers;

File Class.h

vector<Class2*>* get_Another_Vector();

File Class2.h

SDL_Surface* sdlSurface;

File SDL_Surface.h

has the pixels-array:

Uint32 value = *(Uint32*) (* pointer_To_A_Vector_With__Pointers[i]->get_Another_Vector())[i2]->sdlSurface->pixels;

And it should be equivalent to saying this:

   Uint32 *pixels = (Uint32 *)surface->pixels;

It works, but it only retrieves the very first color of the pixel array. But I want to achieve this (the [number] at the very end of the line):

Uint32 value = *(Uint32*) (* pointer_To_A_Vector_With__Pointers[i]->get_Another_Vector())[i2]->sdlSurface->pixels[ number ];

In other words, I want the very last operator[], sdlSurface->pixels[numbers], included.


Solution

  • The precedence of [] is higher than *, so:

     *pointer_To_A_Vector_With__Pointers[i]->get_Another_Vector()
    

    should be:

     (*pointer_To_A_Vector_With__Pointers)[i]->get_Another_Vector()
    

    as your variable name suggests.