What is &screen->clip_rect?

And why is it a memory adress if it is already a pointer?

1
2
3
4
5
6
7
8
//This is the declaration of screen:

SDL_Surface *screen = NULL;

//And this is the code:

SDL_FillRect(screen, &screen->clip_rect, SDL_MapRGB(screen->format, 0xFF, 0xFF, 0xFF));
// What is &screen->clip_rect? And why is it a memory adress if it is already a pointer?? 
I'm not sure so you might want to check yourself, but I think the right arrow operator (->) binds stronger than the reference operator. So you're taking the address of the clip_rect field.
& just returns a pointer to anything you put it next to.

If that thing is an object, you get a pointer to an object.

If it's an int, you get a pointer to an int.

If it's a pointer, you get a pointer to a pointer.

You can even put a & before the pointer-pointer to get a pointer pointing to a pointer that points to a pointer.

I see that SDL_FillRect is declared as SDL_FillRect(SDL_Surface *dst, SDL_Rect *dstrect, Uint32 color);

Therefore, your second argument needs to be a pointer to a rectangle.
In the case of "&screen->clip_rect", -> has a higher priority than & and is therefore evaluated first, it means "follow the screen pointer and access the clip_rect of the pointed-to object." This then returns a reference to an SDL_Rect that you then turn into a pointer by applying & to it.

You could rewrite it as "&(screen->clip_rect)".

I remember pointers and references confused me very much when I started with C++, you'll get used to them, don't worry.
Last edited on
Thanks for the motivation! I totally forgot that the reference operator worked like that.

So basically, when you do this: &screen->clip_rect
You do not automatically get a memory adress back? Which the function wants?
Last edited on
Topic archived. No new replies allowed.