Understanding Pointers In Respect to SDL

Hello all!

I've been trying to grasp the concept of pointers. I thought I had it, until I came back to the usage in SDL. I think it's to do with the way SDL structs work, but I'd like a bit of help understanding!

So basically, why do we need to declare SDL_Surfaces as pointers, like so:

SDL_Surface *screen = NULL;

Moreover, can somebody explain the usage of pointers in the following function's parameters? And also why we use '&offset' instead of just 'offset' with the SDL_BlitSurface();

1
2
3
4
5
6
7
8
9
void apply_surface(int x, int y, SDL_Surface* source, SDL_Surface* destination)
{
	SDL_Rect offset;

	offset.x = x;
	offset.y = y;

	SDL_BlitSurface(source, NULL, destination, &offset);
}


Any help, any at all, will be much appreciated!
Thanks.

EDIT: Wondering whether this should be in the "Beginners" section?
Last edited on
SDL uses a lot of pointers because it's a C library and doesn't have clever features like constructors, exceptions and references.

In C++ we often use references to avoid creating a copy when passing an object to a function. C doesn't have references so it uses pointers instead for the same reason. Another reason could be to allow optional arguments. The second and forth argument of SDL_BlitSurface is optional. If you don't want to pass a SDL_Rect you simply pass a null pointer.
Hm OK, I think I follow you. But what about the use of & in the following case:

1
2
3
4
5
6
SDL_Event occur;
.
.
.

SDL_PollEvent(&occur);


I just CANNOT fathom why you need to use the memory address of occur?! I'm so confused. What is "SDL_Event occur;" actually doing? Creating an object? Variable? I know this is basics but I can't find an explanation, in terms of the syntax of SDL, anywhere.

Sorry, any further help is much appreciated.
G
Last edited on
Yes it creates an object/variable (A variable is simply put a named object).

SDL_PollEvent takes a pointer so that it can modify the object that you pass to the function.

This is how SDL_PollEvent is declared.
 
int SDL_PollEvent(SDL_Event *event);

If SDL_PollEvent instead was declared as
 
int SDL_PollEvent(SDL_Event event);

we would not use & when passing occur to the function but then there would be no way for the function to actually modify occur because the SDL_Event parameter inside the function would be a copy of occur. Making changes to the copy would have no affect on occur.
Hm I don't quite understand either of those declarations. Declared SDL_Event like so:
SDL_Event occur;

And I never wrote SDL_PollEvent like you did. What's the difference? Are you talking about how SDL_PollEvent is declared in the SDL Libs?

Thanks :)
Yes, I mean how it is declared in the SDL library. It's nothing you can/should change.

The important thing to understand is that the SDL_PollEvent function takes a pointer to a SDL_Event object as argument . occur is not a pointer, it has type SDL_Event, so you can't pass it to SDL_PollEvent. When you do &occur this gives you a pointer to occur and that you can pass to the SDL_PollEvent function.

1
2
3
4
5
6
7
SDL_Event occur;

SDL_PollEvent(occur); // Error! SDL_PollEvent expects a SDL_Event*
                      // as argument but occur is a SDL_Event.
                      
SDL_PollEvent(&occur); // Ok! SDL_PollEvent expects a SDL_Event* as argument
                       // and &occur is a SDL_Event* so this is alright. 
OK, but not a pointer in the variable pointer sense, but as in the sense that it merely POINTS to the SDL_Event occur?

And also, in the above example you gave, you say it expects SDL_Event* as an argument, not SDL_Event. But we never declared SDL_Event*, only SDL_Event. What's the explicit difference?

Sorry! x)
Last edited on
I think you should have another look at references.

http://www.cplusplus.com/doc/tutorial/pointers/

Basically it's this:

1
2
3
4
int a = 10; //a is of type int
int *b = nullptr; //b is a pointer to type int
b = a; //Error: Trying to assign int to a int *
b = &a; //b is pointing to the address of a 


Now the function is looking for an address to point to
 
void someFunction( int *ptr );


So we can call it by:

 
someFunction( b );
or someFunction( &a )


Yeah, I understand mostly, and I've ready a dozen different tutorials on pointers and references - but I don't get this ---void someFunction( int *ptr ); --- *ptr is a dereference of a pointer, so surely it's not pointing to anything anymore?!

Ah so confused haha! Once again, thanks for all the help so far!
Last edited on
No, in that context, int *ptr is declaring an argument called ptr, with type int *.
Last edited on
*ptr is a dereference of a pointer

No.

There are three ways to pass and return.
1)Value/Copy
2)Pointer
3)Reference

Pretty much think of parameters as local variables being constructed and you pass the initial values.

1
2
3
4
5
6
void someFunction( int *ptr );


//somewhere in main

someFunction( nullptr );


is almost the same as:

1
2
3
4
5
6
7
8
9
10
11
void someFunction( void );

//somewhere in main

SomeFunction();


void SomeFunction( void )
{
    int *ptr = nullptr;
}


http://www.learncpp.com/ --Check out chapter 7 specifically
http://www.learncpp.com/cpp-tutorial/71-function-parameters-and-arguments/
http://www.learncpp.com/cpp-tutorial/74-passing-arguments-by-address/

giblit wrote:
I think you should have another look at references

This doesn't have anything to do with references.

The terminology in the linked article is rather confused. There is no "reference operator" and the unary& is most often referred to as the "address of" operator, since all it does is produce a pointer to the variable it is applied to.

George951 wrote:
And also, in the above example you gave, you say it expects SDL_Event* as an argument, not SDL_Event. But we never declared SDL_Event*, only SDL_Event. What's the explicit difference?


An SDL_Event* is a pointer to an object of type SDL_Event. An SDL_Event is an object of type SDL_Event. A pointer can be thought of as an address of another object.

If you have an event e of type SDL_Event, then &e produces a pointer to e. You may want to think of it as a temporary unnamed pointer variable.


Last edited on
So when you call the function void someFunction( int *ptr ); you created a new argument called ptr, which is a pointer, as you called the function?

@cire
If you have an event e of type SDL_Event, then &e produces a pointer to e. You may want to think of it as a temporary unnamed pointer variable.


Which is the temp unnamed pointer variable. &e?
So when you call the function void someFunction( int *ptr ); you created a new argument called ptr, which is a pointer, as you called the function?

Yes, in the same way that you do when you call any function or method in C that takes arguments. Arguments in C are passed by value, which means that as you pass them into a function, you create a copy of them. The copy is local to the function.

So, when you pass a pointer to an int into someFunction, then a copy of that pointer is created.

C++ contains a new type of variable called a reference, but SDL is a C library, not a C++ one, so can't use references.
So you don't actually need to declare the pointer, (or whatever it may be), before hand, if you're passing it into a function?

So I would need to say:

1
2
3
4
5
 int* ptr;
.
.
.
someFunction(int *ptr);


I could just use the function? Or is it needed to be declared first?
Um... it sounds like you don't really understand functions and arguments at all. At this point, you'd be better off going back to your textbook and rereading the section on functions so that it's clear.
Yeah, I guess so. It's just all my books (except one) tackle pointers before functions. I'm also trying to grasp SDL. I guess I'm jumping in at the deep end, for sure, but I usually see how the concepts work eventually.

Thanks.

EDIT: at this stage, I'm guessing that the SDL_Rect, or whatever, doesn't take SDL_Rect as an argument, it has to take a pointer as an argument? Hence the use of '&'? My main question thus would be WHY the use of the address? Or can & be used for more than just pointing to an address?
Last edited on
Yeah, I guess so. It's just all my books (except one) tackle pointers before functions.

So, you're trying to use functions in a 3rd-party graphics library, before you've even learned the basics of functions? At what point did that seem like a sensible idea?

at this stage, I'm guessing that the SDL_Rect, or whatever, doesn't take SDL_Rect as an argument, it has to take a pointer as an argument?

Um, I have no idea what you mean by that. What is SDL_Rect? A type? A function? You haven't mentioned it all in any of your posts until now, so I have no context for that statement.

OK, I've done a quick Google search, and it looks as though SDL_Rect is a type - a struct. Which means it doesn't "take" anything. A function might take an SDL_Rect as an argument, or it might take a pointer to an SDL_Rect as an argument, but without knowing what function you're talking about, I couldn't give you an answer as to why.

People have already given several reasons why a function might be defined so as to take a pointer to a struct rather than an actual struct:

1) To avoid the performance hit of copying a large struct when passing it into a function

2) To allow the function to make changes to the struct that will be reflected in the calling code - because the pointer in the calling code and the copy of the pointer in the function are both pointing to the same memory

3) To allow an "optional" parameter, because a pointer can be set to NULL.
Well, I used to know a bit about functions in C++, and I've programmed a lot with Lua. So I thought it might come back to me.

By the way, you've answered my question. Thanks.
Topic archived. No new replies allowed.