Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Array of functions handling in C

Tags:

arrays

c

function

I've declared an array of functions as:

void * (thread_fun[100])(void *);

But, compilation is terminated with error:

error: declaration of ‘thread_fun’ as array of functions void * (thread_fun[])(void *);

What is wrong with my declaration. And, how it can be corrected. I want to create an array of function in my program. Suggest me a solution.

like image 228
Sundeep KOKKONDA Avatar asked Feb 02 '16 14:02

Sundeep KOKKONDA


2 Answers

It's not possible to declare array of functions. You can only declare array of pointers to function:

void * (*thread_fun[100])(void *);
like image 198
Zbynek Vyskovsky - kvr000 Avatar answered Sep 28 '22 01:09

Zbynek Vyskovsky - kvr000


As user Zbynek Vyskovsky noted, you can only have array of function pointers.

However, I would also recommend that you use typedef to make handling of function pointers easier:

typedef void* (*FunctionPtrType)(void*);  // Define type
FunctionPtrType thread_fun[100];          // Declare the array
like image 37
user694733 Avatar answered Sep 28 '22 01:09

user694733