Results 1 to 3 of 3

Thread: C: Function Pointer??

  1. #1

    Thread Starter
    PowerPoster rjlohan's Avatar
    Join Date
    Sep 2001
    Location
    Sydney, Australia
    Posts
    3,205

    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?




    -----------------------------------------
    -RJ
    [email protected]
    -----------------------------------------

  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  3. #3

    Thread Starter
    PowerPoster rjlohan's Avatar
    Join Date
    Sep 2001
    Location
    Sydney, Australia
    Posts
    3,205
    Okey dokey.
    -----------------------------------------
    -RJ
    [email protected]
    -----------------------------------------

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width