我有一个像这样的 typedef 结构:
typedef struct {
int col;
int row;
} move_t;
我正在尝试将类型为 move_t
的数组作为要填充的缓冲区传递给一个函数...像这样:
void generate_valid_moves(int fc, int fr, move_t* moves, int limit);
或
void generate_valid_moves(int fc, int fr, move_t moves[], int limit);
两者都会产生一个模糊的 gcc 错误:
moves.h:43: error: expected declaration specifiers or ‘...’ before ‘move_t’
moves.h:887: error: conflicting types for ‘generate_valid_moves’
moves.h:43: note: previous declaration of ‘generate_valid_moves’ was here
我试过删除 typedef 并将其设为普通结构,等等……所有这些都会导致类似的错误。感觉很基本,我知道我错过了一些东西......
我的函数原型(prototype)和实现的签名完全匹配...所以那部分错误甚至更奇怪。
我的目标是创建一个 move_t
数组,然后将其传递给此函数以填充 move_t
。然后,调用者使用填充的 move_t
缓冲区执行操作。
最佳答案
typedef
需要在引用类型的函数原型(prototype)之前。 C代码是按顺序处理的,你不能在定义之前引用一个名字。因此,如果您首先没有结构定义,那么它会认为 move_t
是一个正在声明的变量,但它之前需要一个类型说明符。
关于c - 将结构指针或数组作为函数参数传递,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52864575/