Thread: Array of function pointers?

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    412

    Array of function pointers?

    Ok, I'm a bit confused as to how to declare this.

    I want to pass into one of my functions an array (of variable size) of function pointers. As it's variable size I assume I need to pass it as a pointer to the beginning of the array + count of elements.

    Now, the syntax for declaring function pointers is already odd, so how do I declare this? Each function would take and return a double.

    Would I declare my function like this:

    void theFunction(double (**pfunc)()),int count)


    And use it like this to call the first function (function #0):

    double d = (*pfunc[0])(0.5);

    ??
    Last edited by The V.; 10-16-2001 at 04:55 PM.

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    412
    Figured it out

    Figured I'd save myself some headache and do something like this:

    typedef double (* FUNCPTR_DD)(double);

    And then use:

    theFunction(FUNCPTR_DD *, int);

  3. #3
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    I sometimes get confused with the notation for passing an array of pointers:
    //definition of instance
    CAccount *array[size];
    //call
    array[0].Function(array,size);
    //Declaration in class
    viod Function(CAccount *[], int);
    //definition of method
    void CAccount::Function(CAccount *a[],int size){ ...}

    It would be even more hellish to deal with an array of pointers to functions. This is where C/C++ ........es me off a little bit. I doubt that it has to be this confusing.
    Last edited by Troll_King; 10-16-2001 at 07:17 PM.

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    412
    The worst is actually trying to allocate the memory with new -- because it is confusing as hell as to where you put the *s and the []s.

    That's why I do love that typedef -- you can define a pointer to a function with the params/returns you want as a new type, then use it like any other type.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  2. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  3. Returning an Array of Pointers to Objects
    By randomalias in forum C++ Programming
    Replies: 4
    Last Post: 04-29-2006, 02:45 PM
  4. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM
  5. 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