Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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}

like image 217
Alexandros Fourtounis Avatar asked Aug 08 '26 11:08

Alexandros Fourtounis


1 Answers

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 };
like image 157
ikegami Avatar answered Aug 10 '26 08:08

ikegami