Pointer to array of pointers to functions

M

merlin100

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
 
K

Keith Thompson

merlin100 said:
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>
 
M

merlin100

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
 
M

Morris Keesan

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]; */
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top