Skip to main content
GameDev.net gamedev.net
🔒 Locked

Help understanding extern char*(*fTab[])(void)

Started by popcorn Jun 30, 2006 at 6:57 PM 7 replies 1.3k views
Original Post
popcorn
popcorn
As the title says how do I interpret the following code: extern char*(*fTab[])(void); and more importantly how do I make use of the above code. Examples and explanations please.
How about them apples?
LessBread
LessBread
At a glance, that appears to be a declaration of an external array of function pointers for functions that accept no arguments and return a pointer to char.

It's quite likely that in the code that follows there is some snippet that looks like:

int index = 0; // (... N)
char *ptr = fTab[index]();
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man
Zahlman
Zahlman
That's the same answer I got. ^^
popcorn
popcorn
How do you know how many elements the array holds?

Would you need something like:

fTab = malloc(5 * sizeof(char *));

somewhere in your code to assign some memory for the array.
How about them apples?
LessBread
LessBread
With an array of function pointers, you're likely better off hard coding the array.

For example:

typedef char * (*FunkT)(void); // this is a function pointer type

FunkT fTab[] = { funk1, funk2, funk3, NULL, NULL, NULL };

where funk1, funk2, funk3 are the names of functions defined elsewhere in the code. These functions should have the same signature as FunkT. If you want to assign additional pointers at runtime, declare some of the fTab[] entries as NULL.

"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man
popcorn
popcorn
Quote:
Original post by LessBread
With an array of function pointers, you're likely better off hard coding the array.

For example:

typedef char * (*FunkT)(void); // this is a function pointer type

FunkT fTab[] = { funk1, funk2, funk3, NULL, NULL, NULL };

where funk1, funk2, funk3 are the names of functions defined elsewhere in the code. These functions should have the same signature as FunkT. If you want to assign additional pointers at runtime, declare some of the fTab[] entries as NULL.



Any particular reason why you are better of hard coding the array as opposed to dynamically assigning memory for the array?

Also is the code in my previous post the correct way for assinging memory to an array of function pointers?

[Edited by - popcorn on July 1, 2006 7:42:27 PM]
How about them apples?
darookie
darookie
Technically, a function pointer isn't the same as a char*, hence your code'd better be
/* define the function pointer type */typedef char * (*myfunc)(void);/* define the type for a table of function pointers */typedef myfunc* myfunc_tbl;/* create a variable for the table */myfunc_tbl fTab;/* allocate a table with 5 entries */fTab = (myfunc_tbl)malloc( 5 * sizeof(myfunc) );/* setup content - assume "foo" and "bar" are function that take no parameters   and return a char* */fTab[0] = foo;fTab[1] = bar;/* etc. *//*technically you would have to release the dynamically allocated memory at some point:*/free(fTab); fTab = NULL;

but, dynamically allocating your function table like that requires you to free the table at some point. If you use a fixed size table, you can save yourself the hassle of tracking its memory by simply having it on the stack.

HTH,
Pat.
LessBread
LessBread
Quote:
Original post by popcorn
Any particular reason why you are better of hard coding the array as opposed to dynamically assigning memory for the array?


It depends on whether the function pointers are known at compile time or not. If they are, for example, the functions they point to are right there in the code, you might as well hard code the array. If they aren't, for example, loading them from a dll, then dynamic memory makes more sense. Since you haven't provided context details, I assumed you were dealing with pointers available at run time.

"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man
popcorn
popcorn
Thanks guys, some really helpful explanations there.
How about them apples?

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.