#include <stdio.h>
typedef struct point{
int x;
int y;
};
void main (void){
struct point pt;
pt.x = 20;
pt.y = 333;
struct point pt2;
pt2.y = 55;
printf("asd");
return;
}
对比 2008
c:\documents and settings\lyd\mis documentos\ejercicio1.c\ejercicio1.c\ejercicio1.c(14) : error C2143: syntax error : missing ';' before 'type'
c:\documents and settings\lyd\mis documentos\ejercicio1.c\ejercicio1.c\ejercicio1.c(15) : error C2065: 'pt2' : undeclared identifier
c:\documents and settings\lyd\mis documentos\ejercicio1.c\ejercicio1.c\ejercicio1.c(15) : error C2224: left of '.y' must have struct/union type
Build log was saved at "file://c:\Documents and Settings\LYD\Mis documentos\ejercicio1.c\ejercicio1.c\Debug\BuildLog.htm"
ejercicio1.c - 3 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
最佳答案
由于问题被标记为 C(而不是 C++),并且编译器是 MSVC 2008,因此您只能使用 C89 语义。这意味着您不能在第一条语句之后的 block 中声明变量。因此,第二个结构变量在那里是不允许的。 (C99 和 C++ 都允许您在 block 中的任何位置声明变量。去告诉 MS 更新他们的 C 编译器以支持 C99。)
您的另一个错误是 main()
返回一个 int
,因此:
#include <stdio.h>
struct point
{
int x;
int y;
};
int main (void)
{
struct point pt;
struct point pt2;
pt.x = 20;
pt.y = 333;
pt2.x = 4;
pt2.y = 55;
printf("asd");
return 0;
}
几小时后:代码中不需要关键字 typedef,因为在右大括号之后和分号之前没有指定名称。这不会阻止它编译;它将引发编译器集挑剔的警告。
关于c - 为什么不编译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3827453/