Jam*_*K89 3 c syntax gcc struct
我在Ubuntu上使用CodeLite,并且为了一些奇怪的原因,每当我尝试使用返回指向结构的指针的函数编译代码时GCC都会抛出此错误:
error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘*’ token
Run Code Online (Sandbox Code Playgroud)
这是我写的一个例子,用于演示此错误:
#include <stdio.h>
typedef struct test_t {
unsigned char someVar;
};
test_t* testFunc() { // GCC throws that error on this line
return NULL;
}
int main(int argc, char **argv)
{
return 0;
}
Run Code Online (Sandbox Code Playgroud)
因此,除非我忘记了一些明显的东西,否则我通常希望这些代码可以在任何其他编译器上编译,即MSVC,所以我完全不知道为什么它不起作用.
希望你们中的一位专家能够赐教.
谢谢!
AnT*_*AnT 10
您定义了一个名为的类型struct test_t.稍后在代码中尝试使用类型test_t.你的程序中没有这种类型.您定义的类型再次被调用struct test_t.这是编译器试图告诉你的内容.
在函数声明中使用类型的正确名称
struct test_t* testFunc() {
return NULL;
}
Run Code Online (Sandbox Code Playgroud)
或者test_t为你的struct test_t第一个定义一个简短的"别名"
typedef struct test_t test_t;
test_t* testFunc() {
return NULL;
}
Run Code Online (Sandbox Code Playgroud)