array of function pointers

This is a discussion on array of function pointers within the C Programming forums, part of the General Programming Boards category; Does anyone know how to do an array of function pointers when its double array?. Code: FIPFunc[ , ] = ...

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    192

    array of function pointers

    Does anyone know how to do an array of function pointers when its double array?.
    Code:
    FIPFunc[ , ] = {&FIPDiscSolicit(), &FIPDiscAdv();
    		&FIPReq(), &FIPACC();
     		&FIPKeAlive(), &FIPViLinks();
    		&FIPVLANReq(), &FIPVLANNot();
    
                   };
    fip.c:1: error: expected expression before ‘,’ token

    I'm pretty sure Im declaring this wrong but All the tutorials online aren't really sure how to do it for double array function pointers

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Well you don't use , to specify that you have two dimensions. You actually specify two dimensions...
    Code:
    int array[ SOMENUMBER ][ SOMEOTHERNUMBER ];
    You aren't doing that.


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

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    20
    Also keep in mind that only one dimension can be unspecified, so foo[ ][ ] is invalid.

  4. #4
    Registered User
    Join Date
    Aug 2009
    Posts
    192
    Okay so i put
    Code:
    int FIPFunc[2][4] = {&FIPDiscSolicit(), &FIPDiscAdv(),
    		&FIPReq(), &FIPACC(),
     		&FIPKeAlive(), &FIPViLinks(),
    		&FIPVLANReq(), &FIPVLANNot()
    
                   };
    But I get a lot of
    error: lvalue required as unary ‘&’ operand
    What does that mean should i not use the & sign

  5. #5
    cas
    cas is offline
    Registered User
    Join Date
    Sep 2007
    Posts
    983
    The name of a function is a pointer to that function (in almost all cases). You should not use () to "remind" the compiler that it's a function, as the compiler already knows that. What you're doing is attempting a function call and applying the & operator to the result. Just use the function name without parentheses. Optionally you can add an & if you think it makes semantic sense.

  6. #6
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Don't really know what an array of int has to do with an array of pointers. Don't you want something like this:
    Code:
    typedef void (*fun)(int, int);
    fun arrayFun[2] = {thisFun, thatFun};
    If the functions don't return the same type and have the same parameters, still wouldn't you want an array of void*
    Code:
    void* arrayFun[2] = {thisFun, thatFun};

  7. #7
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by kiros88 View Post
    I'm pretty sure Im declaring this wrong but All the tutorials online aren't really sure how to do it for double array function pointers
    Yeah, probably <5 people (including the OP) in the history of the universe have felt the need to do such a thing, so surprise surprise there isn't a good "2D function pointer array How-to".
    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

  8. #8
    Registered User
    Join Date
    Aug 2009
    Posts
    192
    Sorry about that i changed it in my code i was getting that error two I also wanted to know when u do a call for the array of functions pointers what do u get back like.
    Code:
    int result2 = (*pt2Function) (12, 'a', 'b');          // C
    in this example whats in result2.... I got this from a website that didnt explain what the result2 is. Is it just like a status either returning -1 if failed or 1 if successful

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by kiros88 View Post
    Sorry about that i changed it in my code i was getting that error two I also wanted to know when u do a call for the array of functions pointers what do u get back like.
    Code:
    int result2 = (*pt2Function) (12, 'a', 'b');          // C
    in this example whats in result2.... I got this from a website that didnt explain what the result2 is. Is it just like a status either returning -1 if failed or 1 if successful
    Well who knows? pt2Function sounds like "a name we needed to make up". It depends on what function was called, and what that function does/returns.

  10. #10
    Registered User
    Join Date
    Aug 2009
    Posts
    192
    So basically the result2 is whatever each of the functions returns does that mean each function call in the array have to return the same type of data back?

  11. #11
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by kiros88 View Post
    I also wanted to know when u do a call for the array of functions pointers what do u get back like.
    A pointer to an array. I think the tutorial you were reading recommends typedef'ing, so it would be an array of a custom type. The typedef is a good idea; function ptr syntax is cumbersome.

    Code:
    #include <stdio.h>
    
    typedef (*FPtr)(int);
    
    int test(int x) {
    	return x;
    }
    
    int main() {
    	FPtr eg[5];
    	int i,r2;
    	for (i=0;i<5;i++) {
    		eg[i] = test;
    		r2 = eg[i](i);
    		printf("%d\n",r2);
    	}
    	return 0;
    }
    Quote Originally Posted by kiros88 View Post
    So basically the result2 is whatever each of the functions returns does that mean each function call in the array have to return the same type of data back?
    Yes and yes, but you could use the flexible "void*".
    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

  12. #12
    Registered User
    Join Date
    Aug 2009
    Posts
    192
    Quote Originally Posted by MK27 View Post
    Yeah, probably <5 people (including the OP) in the history of the universe have felt the need to do such a thing, so surprise surprise there isn't a good "2D function pointer array How-to".
    Since there really isnt that many do u know any good ones to use im trying to call on it now and im getting an error

    error: called object ‘FIPFunc[((unsigned int)hdr->Code, (unsigned int)hdr->Subcode)]’ is not a function

    Code:
    typedef struct FIP_Hdr
    {
    Uint32 Code:16;
     Uint32 Resv1:8;
     Uint32 Subcode:8;
     Uint32 DescLen:16;
      Uint32 DescList;
    } FIP_Hdr;
    
        Uint32 DescListLen;
        Uint8  *DescListPtr;
        DescListLen = (Uint32) hdr->DescLen;
        DescListPtr = (Uint8*)&(hdr->DescList);
        int status = (*FIPFunc[(Uint32) hdr->Code, (Uint32) hdr->Subcode])(DescListPtr,
    DescListLen);
    how do u call on the funtion pointer array isnce its 2-d

  13. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by kiros88 View Post
    Since there really isnt that many do u know any good ones to use im trying to call on it now and im getting an error

    error: called object ‘FIPFunc[((unsigned int)hdr->Code, (unsigned int)hdr->Subcode)]’ is not a function

    Code:
    typedef struct FIP_Hdr
    {
    Uint32 Code:16;
     Uint32 Resv1:8;
     Uint32 Subcode:8;
     Uint32 DescLen:16;
      Uint32 DescList;
    } FIP_Hdr;
    
        Uint32 DescListLen;
        Uint8  *DescListPtr;
        DescListLen = (Uint32) hdr->DescLen;
        DescListPtr = (Uint8*)&(hdr->DescList);
        int status = (*FIPFunc[(Uint32) hdr->Code, (Uint32) hdr->Subcode])(DescListPtr,
    DescListLen);
    how do u call on the funtion pointer array isnce its 2-d
    This is not a 2-D function pointer array question. This is a 2-D array question. Just like every other 2-D array, you use a separate pair of brackets for each dimension, as in [4][3].

  14. #14
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by kiros88 View Post
    Since there really isnt that many do u know any good ones to use im trying to call on it now and im getting an error
    No, in fact I was being serious kiros88, I doubt very many people have ever wanted to do that, so you are sort of on your own.

    But using the typedef, you should be able to do it. If you have not passed around arrays of pointers to arrays or 2d arrays much write a short demo using char *ptrs to get used to the syntax before you start in on the typedef function ptrs.
    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

  15. #15
    Registered User
    Join Date
    Aug 2009
    Posts
    192
    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?

Page 1 of 3 123 LastLast
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, 09: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21