Original Post
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.
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.
/* 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;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?
This topic has been locked by a moderator. New replies are not allowed.
With your permission, GameDev.net uses analytics cookies to understand how people use the platform. You can accept analytics or continue with necessary cookies only. Learn more