c - 将内存分配给指向函数的指针数组

Tom*_*Tom 1 c memory

我希望我的函数创建一个数组并为n指向函数的指针分配内存(例如,没有参数并返回int的函数)并返回指向该数组的指针.

我试过做:

void* f(int n){
   return calloc(int (*arrayName[])(void),n);
}
Run Code Online (Sandbox Code Playgroud)

但我得到一个语法错误.我很陌生c,我试图挖掘一小时如何解决这个问题却没有成功.使用man我想到的页面calloc是要走的路,但我可能错了.

Ker*_* SB 8

让您的生活更轻松,并使用typedefs:

typedef int (*fp)();

fp * f(size_t n)
{
    return calloc(n, sizeof(fp));
}
Run Code Online (Sandbox Code Playgroud)

手卷宣言: int (*f(size_t n))()