这是简单的 C 源文件:
struct data{
int a;
char * b;
double c;
};
struct data mydata;
struct data *ptr;
ptr = &mydata;
ptr->a = 1;
ptr->b = NULL;
ptr->c = 0.1;
当我运行命令时:
clang -fsyntax-only source.c
我有这个输出:
source.c:11:1: warning: type specifier missing, defaults to 'int' [-Wimplicit-int]
ptr = &mydata;
^
source.c:11:1: error: redefinition of 'ptr' with a different type: 'int' vs 'struct data *'
source.c:9:14: note: previous definition is here
struct data *ptr;
^
source.c:13:1: error: unknown type name 'ptr'
ptr->a = 1;
^
source.c:13:4: error: expected identifier or '('
ptr->a = 1;
^
source.c:14:1: error: unknown type name 'ptr'
ptr->b = NULL;
^
source.c:14:4: error: expected identifier or '('
ptr->b = NULL;
^
source.c:15:1: error: unknown type name 'ptr'
ptr->c = 0.1;
^
source.c:15:4: error: expected identifier or '('
ptr->c = 0.1;
^
1 warning and 7 errors generated.
最佳答案
以下四行仅在出现在函数中时才有效:
ptr = &mydata;
ptr->a = 1;
ptr->b = NULL;
ptr->c = 0.1;
(mydata
和 ptr
被理解为全局变量)。
如果您将它们包含在原型(prototype)为 int main()
的函数中,那么一切都会好起来的。 (C 编译器希望找到一个名为 main
的函数,而我给你的原型(prototype)是 C 标准接受的。)
关于c - 简单的 C 结构用法使用 clang -fsyntax-only 生成错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35719241/