下面是简单的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
的函数,我给你的原型是C标准所接受的。)关于c - 简单的C结构用法仅使用clang -fsyntax-only会产生错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35719241/