Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
234 views
in Technique[技术] by (71.8m points)

c++ - How define an array of function pointers in C

I've a little question. I'm trying to define an array of function pointers dynamically with calloc. But I don't know how to write the syntax. Thanks a lot.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The type of a function pointer is just like the function declaration, but with "(*)" in place of the function name. So a pointer to:

int foo( int )

would be:

int (*)( int )

In order to name an instance of this type, put the name inside (*), after the star, so:

int (*foo_ptr)( int )

declares a variable called foo_ptr that points to a function of this type.

Arrays follow the normal C syntax of putting the brackets near the variable's identifier, so:

int (*foo_ptr_array[2])( int )

declares a variable called foo_ptr_array which is an array of 2 function pointers.

The syntax can get pretty messy, so it's often easier to make a typedef to the function pointer and then declare an array of those instead:

typedef int (*foo_ptr_t)( int );
foo_ptr_t foo_ptr_array[2];

In either sample you can do things like:

int f1( int );
int f2( int );
foo_ptr_array[0] = f1;
foo_ptr_array[1] = f2;
foo_ptr_array[0]( 1 );

Finally, you can dynamically allocate an array with either of:

int (**a1)( int ) = calloc( 2, sizeof( int (*)( int ) ) );
foo_ptr_t * a2 = calloc( 2, sizeof( foo_ptr_t ) );

Notice the extra * in the first line to declare a1 as a pointer to the function pointer.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...