How would I declare an array of function pointers in C?

278 Views Asked by At

How would I declare a as a array of 4 pointers to functions with no parameters and which return void? The pointers originally point to functions with names: insert, search, update, and print.

This is the closest I can get to the declaration:

void (*a[4]) () {insert,search,update,print}

6

There are 6 best solutions below

2
Andreas Wenzel On

In ISO C18, the line

void (*a[4]) ();

will declare an array of 4 elements in which each element is a pointer to a function returning void that takes an unspecified number of arguments. If you want to specify that the functions to take no arguments, then you must write (void) instead of ():

void (*a[4])(void);

However, in the upcoming ISO C23, this is no longer necessary, as specifying a function with an unspecified number of arguments is no longer possible (except for variadic functions). In C23, it is therefore sufficient to write () instead of (void). This also applies to ISO C++.

Also, if you want to initialize the array, you must use =, like this:

void (*a[4])(void) = {insert,search,update,print};

In C++, the = is not necessary during initialization, but it is necessary in C (both in C18 and C23).

0
Harith On

You're missing the assignment operator. Change it to:

// Thee may dropeth the 4, shouldst thee so wanteth.
void (*a[4])(void) = { insert , search , update , print };

Note that empty brackets specify an unspecified number of arguments (not necessarily 0). If that's not what you want, specify void.

0
ikegami On

It's so much more readable using a typedef when dealing with function pointers.

typedef void (*MyFuncPtr)( void );

MyFuncPtr a[] = { insert, search, update, print };

Note that no arguments is denoted using (void), not ().

Note that while [4] is ok, [] is sufficient here.

Without, it would be

void (*a[])( void ) = { insert, search, update, print };
0
CPlus On

To add a more step-by-step way of thinking about this, a good way to think about complex C declarations is that they are declared the same way they are used. So ask yourself how you would use (index and call) an array of function pointers?

  • First we would index a[]
  • Then dereference the function pointer to obtain a function *a[]
  • And then we would call (*a[])()

Now to turn this into a declaration:

  • Since the functions return void and take no arguments the declaration is void and add void to the parameters bringing us to void (*a[])(void);
  • Finally add an initializer list = {} as we would a normal array

This leaves us with:

void (*a[])(void) = {insert, search, update, print};
0
tstanisl On

Alternatively, one can use a popular typeof extension (a feature in C23) for convenient and readable syntax.

typeof(void(void)) * a[] = {...};

The a is an array of pointers to void(void) function.

2
sajad99 On

Developers usually not using function pointers. These reasons are:

  • A switch statement, so letting the compiler do the work is preferable.

  • Function pointers are dangerous.

  • Function pointers are too difficult to code and maintain. but you can use:

    return_type (*function_array[4])(argument_list);