Archived

This topic is now archived and is closed to further replies.

Zoney

function pointers?

Recommended Posts

I read an article in the genesis series here on gamedev.net where the author mentions function pointers and I haven''t been able to find it in any tutorials at all... so what I would like to find out is: What are function pointers exactly? How do you use them? Why would you want to use them?

Share this post


Link to post
Share on other sites
Function pointers are exactly what they sound like: pointers to functions. They are useful because you can call functions using a pointer rather than explicitly by name. This makes them useful for things like state machines etc...

This is how you declare and use them:

// declare a function pointer to a function that takes 2 ints
// and a float and returns an int.
int (*f_ptr) (int, int, float);

// a function (notice it follows the form of the pointer we
// just defined.
int afunc(int a, int b, float c) {
// do something
return 32;
}

int main() {
// initialise our function pointer with the address of a function
f_ptr = afunc;

// and to call it...
int result = f_ptr(2, 3, 3.2);

return 0;
}

forgive me if it doesn't work - I haven't tested it....
but that, is the idea. You will find a lot of stuff if you search this forum for function pointers.

Regards,
DeVore

EDIT:
Just found: http://www.newty.de/CCPP/FPT/em_fpt.html

Edited by - DeVore on April 26, 2001 12:22:54 PM

Share this post


Link to post
Share on other sites
Like this:

  
//

// MyFoo.h

//

typedef bool Foo_ReadFunc(unsigned char *pTarget, int num);

void Foo_SetReadFunc(Foo_ReadFunc *pRf);
void Foo_DoSomething(void);


//

// MyFoo.c

//

#include "MyFoo.h"

static Foo_ReadFunc *pReadFunc;

void Foo_SetReadFunc(Foo_ReadFunc *pRf) {
pReadFunc = pRf;

return;
}

void Foo_DoSomething(void) {
unsigned char *pData;
int nSize;
bool bRet;

assert(!pReadFunc);

nSize = 1000;
pData = (unsigned char *)malloc(nSize);

bRet = pReadFunc(pData, nSize);
assert(!bRet);

return;
}


//

// Main.c

//

#include "MyFoo.h"

bool MyReadFunc(unsigned char *nAdress, int nLength) {
int nCount;

for(nCount = 0; nCount < nLength; nCount++) {
nAdress[nCount] = rand() % 255;
}

return false;
}

int main() {
Foo_SetReadFunc(MyReadFunc);
Foo_DoSomething();

return 0;
}


Huh, that got longer than I expected, but this demonstrates a function pointer:

I''m assuming you know what a pointer is (if not; think of your computer''s memory being numbered byte-for-byte and a pointer being such a number - directly said, just a variable specifying a position in your RAM).

Well, a pointer can describe the position of a value in your RAM, of an integer for example. But it can as well point to executable code. In C/C++, you can call such pointer, which in fact begins executing the memory space to which it is pointing.
(And if it''s not pointing to executable code that can be very bad )

In this example, the typedef in Foo.h declares the type of a function (just like typedef unsigned char UCHAR; does with a variable).
Then, Foo.c uses this type to declare a variable - a function pointer: pReadFunc. Using Foo_SetReadFunc() you can let it point to different functions.
When Foo_DoSomething() is called, DoSomething() reserves some memory and executes the function to which the pointer is pointing. It could be pointing to a function reading a file, or one that generates random data, or perhaps one that copies your bios'' contents into the buffer


And, of course, (I''m about to excuse myself as anyone else would do) this code is not thread-safe, the asserts() will not fail if noone sets the pointers to NULL first, etc.

-Markus-

Share this post


Link to post
Share on other sites
hey, thanks for the great help...
I was thinking that maybe function pointers could be useful in scripting
you guys don''t know any resources for someone who would like to create his own script engine do you? I''ve realized that a good rpg-game is almost impossible to do without some sort of scripting...

Share this post


Link to post
Share on other sites
Just to add one other use for function pointers: they are often used for callback functions, where you pass one function a pointer to your callback function and it calls you call back function at the appropriate time. The standard windows windProc function is a good example. You pass the address of this function to CreateWindow, and later on windows calls your windproc with the appropriate parameters whenever there is a message for you to process. Call back functions and function pointers are also used by some enumuration functions.

Share this post


Link to post
Share on other sites

The links section of this site has a lot of stuff about designing and writing scripting languages.


Another use for function pointers. Back "in the day" when writing a software 3d engine was still a big deal(the Voodoo 1 had just come out). I had an engine, the engine stored a function pointer to the triangle drawing routine... when I wanted to switch between the accelerate version and the software version it was a simple reassignment of the pointer. Lots of fun stuff there, and its of course extensible because you can add another function and the code never knows the difference.

Share this post


Link to post
Share on other sites