c - 简单的 C 结构用法使用 clang -fsyntax-only 生成错误

标签 c clang

这是简单的 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;

(mydataptr 被理解为全局变量)。

如果您将它们包含在原型(prototype)为 int main() 的函数中,那么一切都会好起来的。 (C 编译器希望找到一个名为 main 的函数,而我给你的原型(prototype)是 C 标准接受的。)

关于c - 简单的 C 结构用法使用 clang -fsyntax-only 生成错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35719241/

相关文章:

c - 在二维数组中查找 8 个邻居

clang - Clang 如何找到它的默认 sysroot/target 三元组? (clang-tidy 已停止工作)

c - x86 addl 与 subl

c++ - 为嵌套模板类声明 operator==

c++ - MinGW-Clang 的 libgcc_s_dw2-1.dll 丢失了吗?

android - 使用 NDK cmake 链接静态库 libm.a 或 libc.a

c - 垃圾附加在输出字符串 C 之后的末尾

收集2 : ld terminated with signal 11 [Segmentation fault] in qemu arm ubuntu disk image based compile

ios - 有没有办法在 swift 或 ObjC 中为 IPv4 和 IPv6 发出单独的 DNS 请求