-
C: Function Pointer??
I'm having considerable trouble getting some function pointers to work. The idea is, that a function has an array of strings, it wants to pass the first element to a function, which will check it, and return the address of a function based on what that element is. The returned function should take an array of strings as a parameter.
I've got a function definition:
int (*checkBuiltInts(char s[]))(char *p[])
which I want to return the address of one of several functions defined as:
int functionToReturn(char *p[])
In the calling function, I have:
char **args
int (*builtInFunction(char *params[]))
I am trying to use
builtInFunction = checkBuiltIns(args)
but I get "invalid lvalue in assignment" error on that line...
anyone able to solve this for me?
:confused: :confused: :( :confused: :confused:
-
I wonder that "int (*checkBuiltInts(char s[]))(char *p[])" compiles at all. "char **args
int (*builtInFunction(char *params[]))" this too.
Your definitions confuse me. I suggest using typedefs.
typedef int (*ReturnedFunc)(char *[]);
Declare the function that returns a function pointer like this:
ReturnedFunc GetHandlerFunc(whatever);
Use it like this:
ReturnedFunc pFunc = GetHandlerFunc(whatever);
Then call the returned functions like this:
int i = pFunc(arStr);
Some people prefer this syntax, but htey are equivalent:
int i = (*pFunc)(arStr);
Always use typedefs when messing with function pointers, else it gets too confusing.
-