Sign in to follow this  

Understanding code

This topic is 3109 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm nub at coding and I thought it would be better to understand what it is I'm writing rather than just memorizing steps and taking steps from tutorials. As most of you already know Lazy Foo's popular SDL gaming tutorial. I've understood most of code except for a function that I just can't seem to wrap my head around. He calls a function: SDL_Surface *load_image(string filename) From what I know, I'm guessing this is a pointer to another function that takes arg filename and returns a surface. I did some research and also found out that a candidate for the function it's actually pointing to is SDL_LoadObject. What I don't seem to understand is, how does the compiler know it's actually pointing to that function without that function ever being called in the code?

Share this post


Link to post
Share on other sites
Quote:

From what I know, I'm guessing this is a pointer to another function that takes arg filename and returns a surface.

Not quite. It is a function that takes a parameter "filename" and returns a pointer to a SDL_Surface. This is a concrete function, not a function pointer, so the compiler (or, more specifically the linker) will patch up any references to this function when compiling your code.

To declare a function pointer like you described, it would look like this:

SDL_Surface (*function_pointer)( std::string );

Notice that the name of the function and the asterisk are together in parentheses. This kind of function pointer would need to be pointed at something so you could call it.

To make a function pointer that takes a string and returns a surface pointer (in other words, one that is compatible with load_image):

SDL_Surface *(*function_pointer)( std::string );

Notice that there are two asterisks in the above.

My advice: don't worry too much about function pointers for the moment. The syntax is tricky (although easy when you get the hang of it) and they are not necessary to learn when you are still a self-described "nub". [grin]

Quote:

I did some research and also found out that a candidate for the function it's actually pointing to is SDL_LoadObject. What I don't seem to understand is, how does the compiler know it's actually pointing to that function without that function ever being called in the code?

SDL_LoadObject is an advanced function. The compiler doesn't do anything, you would have to do the pointing yourself. But as I said, this is an advanced topic so I won't go into detail here.

Share this post


Link to post
Share on other sites
Quote:
Original post by Holland
good job rip-off. you've confused him by talking about function pointers.

That's a bizarre comment. It's hard to answer a question involving function pointers without, you know, talking about function pointers.

Share this post


Link to post
Share on other sites

This topic is 3109 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this