c - 为什么不编译

标签 c compiler-errors

#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/

相关文章:

c - 未初始化的指针变量有什么用?

c - 如何在循环中设置信号检查?

计算加类费我的公式正确吗?

C# CS0079 事件处理编译错误

perl - Perl 中是否可以要求进行子例程调用?

c - 将代码从 ATmega32 修改为 Arduino Mega - 函数 `main' 中出现错误,即使没有

javascript - 从 C 调用 JavaScript 函数

c - 该程序适用于小数据输入,但在输入大字符串时会出现运行时错误..?为什么..?

c++ - 意外无法访问的基础(第一个派生类)

c++ - 进程终止,状态为-1073741510