Reputation: 9643
I want my function to create an array and allocate memory for n
pointers to functions (for example, functions that have no parameters and return int) and to return a pointer to that array.
I tried doing:
void* f(int n){
return calloc(int (*arrayName[])(void),n);
}
But i'm getting a syntax error. I'm pretty new to c
and i tried to dig for an hour how to solve this issue with no success. using the man
page i figured calloc
is the way to go but i might be wrong.
Upvotes: 1
Views: 194
Reputation:
Or if you don't want a typedef (hint: you still do want a typedef, just for completeness): wrap the type in a sizeof()
:
return calloc(n, sizeof(int (*)(void)));
Upvotes: 2
Reputation: 476980
Make your life easier and use typedef
s:
typedef int (*fp)();
fp * f(size_t n)
{
return calloc(n, sizeof(fp));
}
The hand-rolled declaration: int (*f(size_t n))()
Upvotes: 7