How Define an Array of Function Pointers in C

How define an array of function pointers in C

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.

c - initialize an array of pointers to functions

If the function you want to supply as the default contents of the array is called func, then

  1. you better use a typedef,
  2. you have to use an array initializer

Consider:

typedef int (*IntFunc)(void);
IntFunc fparr[5] = { func, func, func, func, func };

Or the less readable way, if you prefer to avoid typedef:

int (*fparr[5])(void) = { func, func, func, func, func };

How to use typedef to an array of function pointers

It is generally cleaner to typedef function types, not function pointers. It results in cleaner syntax:

typedef int collection_f(int, int);

Now you can define collection, simply as an array of pointer to collection_f.

collection_f* collection[2] = {&fct1,&fct2};

A typical call syntax would be:

collection[0](1,2);
collection[1](1,2);

Don't de-reference function pointer prior to calling. Actually, call operator takes a function pointer as an operand, not a function. Functions decay to function pointer in all context except & operator ... which returns a function pointer.

Next, I'm not sure what:

alternateName p = &collection[2];

is supposed to mean. I assume that you want p to point to the first element of collection. Moreover, indexing p[1] and p[2] looks like out of bound access because accessing collection is only defined for indices 0 and 1.

Now your code could be rewritten can:

collection_f** p = collection; // pointer to first element which is a pointer to collection_f
int result1 = p[0](1,2);
int result2 = p[1](1,2);
printf("results are: %d, %d",result1, result2);

I hope it fixes the problem.

Array of function pointers in C

  1. First off, you should learn about cdecl:

    cdecl> declare a as array 10 of pointer to function(void) returning pointer to void
    void *(*a[10])(void )
  2. You can do it by hand - just build it up from the inside:

    a

    is an array:

    a[10]

    of pointers:

    *a[10]

    to functions:

    (*a[10])

    taking no arguments:

    (*a[10])(void)

    returning void *:

    void *(*a[10])(void)

  3. It's much better if you use typedef to make your life easier:

    typedef void *(*func)(void);

    And then make your array:

    func a[10];

How to user define array of function pointers which have same prototype as main function?

The type of main (in the form you want) is int main(int, char **).

A pointer to that is int (*main)(int, char **).

An array of those is int (*main[])(int, char **).

A typedef of that is typedef int (*mainPtr[])(int, char **);.

Whether you need a size for the array depends on how you will use the type. If you define and initialize an object of this type, its array size will be completed by counting the initializers. For example:

mainPtr p = { main, NULL };

will create an array with two elements.

In other uses, such as declaring a function parameter with this type, you may not need the array to be complete. An array function parameter is automatically adjusted to be a pointer, so the size is discarded anyway. However, if you wish, you could include the size in the typedef.

Array of Function-Pointers with different functions return value (in C)

You can do it like this:

ret = ( ( int (*)(int,int) ) array[1] )(5,7);

You need to cast to pointer to function type with the correct signature.

assigning function pointer to array of function pointers

You may not initialize an array with the static storage duration with a non-constant object.

Either declare the array like

void (*stateTable2[]) (void) = { function1, function2 };

or move the declaration

void (*stateTable2[]) (void) = {function1,fptr};

inside main making the array as an array with automatic storage duration.

Here is a demonstrative program.

#include <stdio.h>

void function1 (void) // Function definition
{
printf( "Hello " );
}

void function2 (void) // Function definition
{
puts( "World!" );
}

void (*fptr)(void) = function2;

int main(void)
{
void ( *stateTable2[] ) (void) = { function1, fptr };
void(*fp)(void) = fptr;

fp();

for ( size_t i = 0; i < sizeof( stateTable2 ) / sizeof( *stateTable2 ); i++ )
{
stateTable2[i]();
}

return 0;
}

Its output is

World!
Hello World!

Pointer to an array of function pointers

Ah, tricky tricky!!!

I think this works

int (*(*rdPtrList)[4])(unsigned int addr, unsigned int data);

because the compiler tells me _countof(*rdPtrList) is 4.


(I wish you could just say int function(unsigned int addr, unsigned int data)[4]* like you can in D, it's so much more readable: it would be a "function array pointer".)



Related Topics



Leave a reply



Submit