use of pointers in lesson#6

Started by
10 comments, last by dnagcat 21 years, 1 month ago
I have some specific pointer questions in lesson 6 that I need some help with...These lines of code are new functions Jeff added to this lesson... AUX_RGBImageRec *LoadBMP(char *Filename) {...} I am assuming that LoadBMP is a function pointer that returns AUX_RGBImageRec...right? So why use a function pointer when this function is called like so... if(TextureImage[0]=LoadBMP("Data/NeHe.bmp"){...} by the way TextureImage[0] is also a pointer... AUX_RGBImageRec *TextureImage[1]; Im a little lost with all the pointers being thrown around! Maybe I answered my own question... LoadBMP("Data/NeHe.bmp") returns NULL or auxDIBImageLoad("Data/NeHe.bmp")...so if it passes then A pointer of AUX_RGBImageRec is NULL or is equal to auxDIBImageLoad(Filename)... No Im more confused now!
Advertisement
a function pointer example:

typedef (void)(*Function)();

a function (body) returning a pointer to an object example:

char *Function() { };

Crispy

"Literally, it means that Bob is everything you can think of, but not dead; i.e., Bob is a purple-spotted, yellow-striped bumblebee/dragon/pterodactyl hybrid with a voracious addiction to Twix candy bars, but not dead."- kSquared
What a pointer does is tell you where a variable/data is being held. When you set a pointer equal to another pointer, it will point to the data which the other pointer is pointing to.

For example:

  int a;        //you have a variableint *b;       //and you have a pointerint *c;       //and a second pointerb=&a         //now you set b to point to int ac=b;          //now you tell c to point to what b was pointing to  


Using pointers here is a bit quicker than just making a new variable and transfering the data to it, but it works just the same.

All the null means is that the pointer has no data to point to and signifies an error.
thanks for the replies so far...but I know what a pointer is and what a pointer does already...and Crispy either I don''t get what your saying or you didn''t make any sense...lol one or the other!

My question was specifically with the calls
AUX_RGBImageRec and auxDIBImageLoad. So could anyone answer those specific questions...sorry if I sound rude. I don''t mean to be.
Since auxDIBImageLoad is being returned from AUX_RGBImageRec *LoadBMP(char *Filename), I would assume it looks like this:

AUX_RGBImageRec *auxDIBImageLoad(char *)

AUX_RGBImageRec is a variable name just like int in my example above (but more complex)

What is happening here is this:

1. auxDIBImageLoad is returning a pointer of type AUX_RGBImageRec
2. then LoadBMP is returning that pointer to your variable
3. since your variable is also a pointer, it now points to some data you can''t see in auxDIBImageLoad()

you could have called
TextureImage[0]=auxDIBImageLoad("Data/NeHe.bmp") to get the same thing, but it will not check to see if there are errors.
instead of reading:

AUX_RGBImageRec *auxDIBImageLoad(char *)

read:

AUX_RGBImageRec* auxDIBImageLoad(char *)

| - Project-X - my mega project.. getting warmer - | - adDeath - an ad blocker I made - | - email me - |
quote:Original post by dnagcat
...and Crispy either I don''t get what your saying or you didn''t make any sense...lol one or the other!


I''m afraid it''s the former in this case

I think NeHe doesn''t use function pointers in his tutorials.

To explain the first line of code in my previous post (the second has been explained - RipTorn was pretty clear about it):

typedef (void)(*Function)();

creates a pointer to a memory address that is treated as a function entry point that returns void and has no arguments. To use it:


  typedef (void)(*Function)();void RenderSky(){}void RenderTerrain(){}int main(int argc, char * args[]){Function CurrentFunction;argc > 1 ?  CurrentFunction = RenderSky :  CurrentFunction = RenderTerrain;//call whichever was selectedCurrentFunction();}  


Crispy
"Literally, it means that Bob is everything you can think of, but not dead; i.e., Bob is a purple-spotted, yellow-striped bumblebee/dragon/pterodactyl hybrid with a voracious addiction to Twix candy bars, but not dead."- kSquared
Now I am confused. As far as I can see, there are no function pointers in lesson 6 (at least that part). There are functions that return pointers to which I think dnagcat was reffering. So what exactly don''t you understand, dnagcat?
I misunderstood how the function was written... I had never come across or used function pointers before and it looked as if AUX_RGBImageRec *LoadBMP(char *Filename) {...;} was a function pointer...

But I guess if it were then it would have been written as
AUX_RGBImageRec (*LoadBMP)(char *Filename)

and as far as I can tell you would use it like this:
AUX_RGBImageRec a(char *Filename){...;}
LoadBMP=a; //function pointer points to a()
(*LoadBMP)(char *Filename); //or LoadBMP(char *Filename);

so basically by calling LoadBMP() you actually call a()...OK fine

so I really don''t understand why the * is written before LoadBMP...if AUX_RGBImageRec is a pointer to LoadBMP(char *Filename) why wouldn''t it be written like this...

AUX_BMPImageRec LoadBMP(char *Filename){...;}
int main()
{
AUX_RGBImageRec *Ptr;
Ptr = LoadBMP;
}

What the hell this is driving me crazy!!!! HAAAAAAA
Hi There,
Quoting RipTorn here:
instead of reading:
quote:
AUX_RGBImageRec *auxDIBImageLoad(char *)

read:

AUX_RGBImageRec* auxDIBImageLoad(char *)

This IS the answer to your question.
1)LoadBMP() is a function whose:
Function Args: char* Filename (or char *Filename)
==> an array of characters, a string..or whatever else you like to call it.
Return Value :AUX_RGBImageRec* (or AUX_RGBImageRec *)
==>It returns a pointer to a structure of type AUX_RGBImageRec!

You say:
quote:
AUX_RGBImageRec *LoadBMP(char *Filename) {...;} was a function pointer...

This is WRONG! LoadBMP is NOT a function pointer. It *returns* a pointer.

2)TextureImage is declared as: AUX_RGBImageRec *TextureImage[1];
or equivalently: AUX_RGBImageRec* TextureImage[1];(depends on what style you use)
==>It can hold the address of a variable of type AUX_RGBImageRec
==>It can be used to hold the return value of function LoadBMP which is of type.......pointer to structure of type AUX_RGBImageRec

Now read what xiros wrote:
quote:
What is happening here is this:

1. auxDIBImageLoad is returning a pointer of type AUX_RGBImageRec
2. then LoadBMP is returning that pointer to your variable
3. since your variable is also a pointer, it now points to some data you can't see in auxDIBImageLoad()

you could have called
TextureImage[0]=auxDIBImageLoad("Data/NeHe.bmp") to get the same thing, but it will not check to see if there are errors.


Does this make any sense? Sorry for the rather extended application.I know ....I know that you know what pointers are, hence, my apologies to you in advance.

Welcome to the club in case you are still confused! I ALWAYS am.

"Recovering Thinker"
~~~~~~~~~~~~~~~~~~~~~~~~
Believe in yourself!
~~~~~~~~~~~~~~~~~~~~~~~~







[edited by - inrecovery on March 18, 2003 4:51:42 AM]

[edited by - inrecovery on March 18, 2003 4:52:26 AM]

This topic is closed to new replies.

Advertisement