ecrouse
asked on
Building menus from Arrays of Pointers to Functions
I am using standard C to construct a menu. The menu will have several options
that call the appropriate function. If I want to build a menu with an array
and let the array contain pointers to the function and also assuming the
functions in general are of different types the I will run into trouble because the array only like things of the same type. How does one create something like this using <ordinary> C.Just looking for methodology. Thanks
I can imagine two possibilities. Either the functions are all very different, or they're not.
If the functions are very different, that is they return different kinds of things, and take different argument lists, then the advantage of putting a series of pointers to them in an array would appear to be lost. You are going to need some kind of switch anyway, in order to deal with the returns and/or argument lists.
If the functions are all very similar then the array problem disappears all together. I have code in which there is a series of function pointers in an array for a menu. Each of the functions returns void, and takes an integer as an argument. This works very well (in my situation anyway).
If the functions are very different, that is they return different kinds of things, and take different argument lists, then the advantage of putting a series of pointers to them in an array would appear to be lost. You are going to need some kind of switch anyway, in order to deal with the returns and/or argument lists.
If the functions are all very similar then the array problem disappears all together. I have code in which there is a series of function pointers in an array for a menu. Each of the functions returns void, and takes an integer as an argument. This works very well (in my situation anyway).
you could also change your function definitions to all return void, then add an argument to each of them that is pointer to the return variable, and set that within the function instead of actually returning a value.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Sweet Description/Example Honey_Hamster!
ASKER
Sweet Description/Example Honey_Hamster?!
1) declare your array to be of type void* (the general pointer type).. but you might need to keep track of which type it really is in another array, and then cast it.. i'm not sure
2)to get the pointer to the functions just use address operator on it
3) then just populate your array
see what happens.