[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c

Pointer to array of pointers to functions

merlin100

3/24/2011 8:09:00 PM

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
3 Answers

Keith Thompson

3/24/2011 8:57:00 PM

0

merlin100 <aceman500@gmail.com> 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) kst-u@mib.org <http://www.ghoti.ne...
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

merlin100

3/24/2011 9:59:00 PM

0

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

Morris Keesan

3/25/2011 3:36:00 AM

0

On Thu, 24 Mar 2011 16:08:34 -0400, merlin100 <aceman500@gmail.com> 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 -- mkeesan@post.harvard.edu