c 中 `void *ptr[N](int)` 和 `void (*ptr)[N](int)` 有什么区别?

问题描述 投票:0回答:1

假设我有

void (*ptr[3])(int)
void (*ptr)[3](int)

第一个按预期工作。 但第二个抛出错误。

我都尝试了,但无法找出问题所在。 错误如下:

“..错误:将‘ptr’声明为函数数组 void (*ptr)3;"

c function pointers operator-precedence
1个回答
2
投票

void (*ptr)[3](int)
与声明
ptr
为函数数组相同,这是不允许的。

typedef void(functype)(int);
functype ptr[3];              // error

您需要的很可能是一个函数数组指针

typedef void(functype)(int);
functype* ptr[3];
//      ^

使用示例:

void a(int x) {}
void b(int x) {}
void c(int x) {}

int main(void) {
    typedef void(functype)(int);

    functype* ptr[3] = {a,b,c};
}
© www.soinside.com 2019 - 2024. All rights reserved.