Pointer to array of pointers to functions

Discussion in 'C Programming' started by merlin100, Mar 24, 2011.

  1. merlin100

    merlin100 Guest

    Ok I have an array of pointers to functions that return int and take
    void like:

    int (*funcsP[])(void) = {func_a, func_b};

    Now I want to create a pointer to that array and I can't figure out
    the correct "incantation" :)

    Any help would be appreciated.

    Ace
     
    merlin100, Mar 24, 2011
    #1
    1. Advertising

  2. merlin100 <> writes:
    > Ok I have an array of pointers to functions that return int and take
    > void like:
    >
    > int (*funcsP[])(void) = {func_a, func_b};
    >
    > Now I want to create a pointer to that array and I can't figure out
    > the correct "incantation" :)
    >
    > Any help would be appreciated.


    Are you sure you want a pointer to the array? Usually it's more useful
    to have a pointer to an element of the array; by incrementing that
    pointer, you can step through the array.

    For a pointer to the entire array:

    int (*(*arr_ptr)[2])(void) = &funcsP;

    For a pointer to an element of the array:

    int (*(*elem_ptr))(void) = funcsP;

    The "cdecl" command is very useful for this kind of thing:

    % cdecl
    Type `help' or `?' for help
    cdecl> explain int (*funcsP[])(void)
    declare funcsP as array of pointer to function (void) returning int
    cdecl> declare arr_ptr as pointer to array of pointer to function (void) returning int
    Warning: Unsupported in C -- 'Pointer to array of unspecified dimension'
    (maybe you mean "pointer to object")
    int (*(*arr_ptr)[])(void )
    cdecl> declare arr_ptr as pointer to array 2 of pointer to function (void) returning int
    int (*(*arr_ptr)[2])(void )
    cdecl> declare elem_ptr as pointer to pointer to function (void) returning int
    int (**elem_ptr)(void )
    cdecl>

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Mar 24, 2011
    #2
    1. Advertising

  3. merlin100

    merlin100 Guest

    You are a genius Keith, a pointer to the first element of the array is
    what I needed and it works

    and thanks for the cdecl tip it's great I love it!



    > For a pointer to an element of the array:
    >
    >     int (*(*elem_ptr))(void) = funcsP;


    Thats the ticket. I don't know why I did not try that..

    Thanks again!
    Ace
     
    merlin100, Mar 24, 2011
    #3
  4. On Thu, 24 Mar 2011 16:08:34 -0400, merlin100 <> wrote:

    > Ok I have an array of pointers to functions that return int and take
    > void like:
    >
    > int (*funcsP[])(void) = {func_a, func_b};
    >
    > Now I want to create a pointer to that array and I can't figure out
    > the correct "incantation" :)


    Rather than giving yourself headaches trying to deal with C's messy
    syntax for function pointers, consider using a typedef:

    typedef int (*funcp)(void); /* funcp is ptr to func(void) returning int */

    funcp funcsP[] = {func_a, func_b};

    funcp *funcp_ptr = funcsP;
    /* Or equivalently, funcp *funcp_ptr = &funcsP[0]; */


    --
    Morris Keesan --
     
    Morris Keesan, Mar 25, 2011
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. mann!

    pointer to array v/s array of pointers

    mann!, Feb 25, 2005, in forum: C Programming
    Replies:
    6
    Views:
    337
    E. Robert Tisdale
    Feb 26, 2005
  2. Marc Thrun
    Replies:
    15
    Views:
    865
    Tim Rentsch
    Oct 4, 2005
  3. newbie

    Pointers to char pointers in functions

    newbie, Sep 18, 2006, in forum: C Programming
    Replies:
    9
    Views:
    310
    August Karlstrom
    Sep 24, 2006
  4. cerr

    pointers, pointers, pointers...

    cerr, Apr 7, 2011, in forum: C Programming
    Replies:
    12
    Views:
    688
  5. , India

    pointer to an array vs pointer to pointer

    , India, Sep 20, 2011, in forum: C Programming
    Replies:
    5
    Views:
    460
    James Kuyper
    Sep 23, 2011
Loading...

Share This Page