Thread: array of function pointers

  1. #16
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Use the TYPEDEF!
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  2. #17
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Did you ever get the declaration of your array fixed?

  3. #18
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by kiros88 View Post
    Code:
    int status = (FIPFunc[(Uint32) hdr->Code] [(Uint32) hdr->Subcode])(DescListPtr, DescListLen);
    So i put that with the brackets in the right order but i still keep getting the error
    error: called object ‘FIPFunc[(unsigned int)hdr->Code][(unsigned int)hdr->Subcode]’ is not a function
    which its not a function cuz Its suppose to be an array?
    So what does this really mean? You are trying to create a 2D array and also an array of pointers to functions that return an integer. You can do either one or the other but not both.
    Code:
    int status = (*FIPFunc)(arg1, arg2);   /* FIPFunc is a pointer to a function that takes 2 arguments and returns an int */
    int (*FIPFunc[10]) (arg1, arg2);       /* this is an array of pointers to functions that take 2 arguments and return an int */

  4. #19
    Registered User
    Join Date
    Aug 2009
    Posts
    192
    Quote Originally Posted by MK27 View Post
    Use the TYPEDEF!
    Sorry bout that didn't understand until i read more on it but i tried
    Code:
    typedef void(*FIPF)(Uint8,Uint32);
    int status = (FIPFunc[(Uint32) hdr->Code] [(Uint32) hdr->Subcode])(DescListPtr, DescListLen);
    And theres no such luck im not sure how to typedef it since its 2-D

    And I did get declaring it to work since i used a
    void* FIPFunc.......
    and the void* allowed it to compile ......

  5. #20
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by kiros88 View Post
    And theres no such luck im not sure how to typedef it since its 2-D
    No no no no no! You typedef the (singular) function ptr prototype you want to use, and then you can create any kind of array you want with it:
    Code:
    typedef (*FPtr)(int);
    FPtr fray[10][10];
    fray[0][0]=myfunc;
    fray[0][1]=someotherfunc;
    ...etc.
    Nice, tidy, simple.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #21
    Registered User
    Join Date
    Aug 2009
    Posts
    192
    Code:
    typedef (*FIPF)(int);
    FIPF FIPFunc[4][2];
    
    void FIPinit(){
      FIPFunc[0][0] = &FIPDiscSolicit;
    }
    
    int FIPDiscSolicit(Uint8 *DescListPtr, Uint32 DescListLen)
    {
    }
    I tried this out but i get this warning
    fip.c:19: warning: assignment from incompatible pointer type
    The warning is in the FIPInit() but iono whats wrong wit the pointer type

    O yea and I'm having a problem calling it now
    int status = (FIPFunc[(Uint32) hdr->Code] [(Uint32) hdr->Subcode])(DescListPtr, DescListLen);
    I get
    too many arguments to function ‘FIPFunc[(unsigned int)hdr->Code][(unsigned int)hdr->Subcode]’

    UHH YES IT COMPILED iono if really works but at least it compiled im going to test it around now see if its all okay
    Last edited by kiros88; 09-15-2009 at 03:33 PM. Reason: IT COMPILED

  7. #22
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Why are you using the address-of operator?


    Quzah.
    Hope is the first step on the road to disappointment.

  8. #23
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    So why not do the typedef to match what it's supposed to be?
    Code:
    typedef int (*FIPF)(Uint8 *, Uint32);
    (Edit: And if you haven't prototyped the function, the compiler won't know that it's a function either....)

  9. #24
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Out of curiosity, why do you want a 2D array of function pointers? What are you trying to do here?

  10. #25
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by quzah View Post
    Why are you using the address-of operator?
    Quzah.
    Ditto. kiros88, I gave you an example: do you see & there? You have to pay attention to details or they will just bury you.

    Also, the prototypes need to match, look:
    Code:
    typedef (*FIPF)(int);  /* this function has one arg, an int */
    int FIPDiscSolicit(Uint8 *DescListPtr, Uint32 DescListLen) /* this function has two args, niether of them int */
    BTW, for a non int function you need to include the type:
    Code:
    typedef char *(*FPtr)(int);
    returns char *ptr.
    Last edited by MK27; 09-15-2009 at 03:56 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  11. #26
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    BTW, for a non int function you need to include the type:
    I see no reason why you would ever leave the type off (especially since implicit int disappeared in C99).

    Also, as a more general question, what's the problem with using the address-of operator on a function? I don't do it myself, but there's nothing wrong with it. It makes some sense to use it, I think, to make it obvious what the code is doing. It's somewhat akin to using &array[0] instead of array; both work just fine and mean the same thing (again, in most contexts). Unless I'm missing something obvious here, of course.

  12. #27
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Yeah,
    a and &a[0] are the same, but are not the same with &a, which I believe was the error here

  13. #28
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    Quote Originally Posted by C_ntua View Post
    Yeah,
    a and &a[0] are the same, but are not the same with &a, which I believe was the error here
    But that's the case for an array, not a function. &func and func are the same thing (except in a couple of corner cases).

  14. #29
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by cas View Post
    But that's the case for an array, not a function. &func and func are the same thing (except in a couple of corner cases).
    C: A Reference Manual, 5th ed, page 208:
    The name of a function evaluates to that function; it is not an lvalue. Unless the function name is the argument of the address operator (&) or the argument to sizeof, the name is converted to a pointer to the function as part of the usual unary conversions. The result of &f is a pointer to f, not a pointer to a pointer to f, and sizeof(f) is invalid.

    [edit]
    In case anyone cares...

    C: A Reference Manual, 5th ed, page 167:
    An expression of type "function returning..." that is not used in a function call, as the argument of the address operator, &, or as the argument of the sizeob operator is immediately converted to the type "pointer to function returning...." (Not performing the conversion when the function is the argument of sizeof ensures that the sizeof expression will be invalid and not just result in the size of a pointer.) The ony expressions that can yield a value type of "function returning T" are the name of such a function and an indirection expression consisting of the unary indirection operator, *, applied to an expression of type "pointer to function returning...."

    Example
    The following program assigns the same pointer value to fp1 and fp2:

    extern int f();
    int (*fp1)(), (*fp2)();
    fp1 = f; /* implicit conversion to pointer */
    fp2 = &f; /* explicit manufacture of a pointer */

    [/edit]

    Quzah.
    Last edited by quzah; 09-15-2009 at 07:37 PM.
    Hope is the first step on the road to disappointment.

  15. #30
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    Right. When I say corner cases I mean you can't apply & to &func (which is pretty obvious), and you can't apply sizeof to func (which is less obvious). In other cases, it's treated as a pointer to the function. So when you're passing a function (pointer) to another function, or assigning it to a function pointer variable, and so on, you can either use the & or not. Both are valid.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  2. Returning an Array of Pointers to Objects
    By randomalias in forum C++ Programming
    Replies: 4
    Last Post: 04-29-2006, 02:45 PM
  3. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM
  4. Passing a 2d Array of pointers to a Function
    By miclus in forum C Programming
    Replies: 6
    Last Post: 09-11-2004, 07:34 AM
  5. Array of function pointers?
    By The V. in forum C++ Programming
    Replies: 3
    Last Post: 10-16-2001, 08:37 PM