Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Pointer to array of pointers to functions

Reply
Thread Tools

Pointer to array of pointers to functions

 
 
merlin100
Guest
Posts: n/a
 
      03-24-2011
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
 
Reply With Quote
 
 
 
 
Keith Thompson
Guest
Posts: n/a
 
      03-24-2011
merlin100 <(E-Mail Removed)> 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.velocityreviews.com/forums/(E-Mail Removed) <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"
 
Reply With Quote
 
 
 
 
merlin100
Guest
Posts: n/a
 
      03-24-2011
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
 
Reply With Quote
 
Morris Keesan
Guest
Posts: n/a
 
      03-25-2011
On Thu, 24 Mar 2011 16:08:34 -0400, merlin100 <(E-Mail Removed)> 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 -- (E-Mail Removed)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
pointer to an array vs pointer to pointer subramanian100in@yahoo.com, India C Programming 5 09-23-2011 10:28 AM
pointers, pointers, pointers... cerr C Programming 12 04-07-2011 11:17 PM
Pointers to char pointers in functions newbie C Programming 9 09-24-2006 10:31 AM
Questions about pointers to objects and pointers to functions Marc Thrun C Programming 15 10-04-2005 05:47 PM
pointer to array v/s array of pointers mann! C Programming 6 02-26-2005 12:59 AM



Advertisments