C语法错误: missing ';' before 'type'

标签 c syntax-error visual-c++-2010

我在 Microsoft Visual C++ 2010 Express 中执行的 C 项目中遇到了一个非常奇怪的语法错误。我有以下代码:

void LoadValues(char *s, Matrix *m){
    m->columns = numColumns(s);
    m->rows = numRows(s);
    m->data = (double*)malloc(sizeof(double) * (m->rows * m->columns));
    int counter = 0;
    double temp;
    bool decimal;
    int numDec;
    while(*s != '\0'){
        .
        .
        .
    }
}

当我尝试构建解决方案时,我得到了“缺失的‘;’”我的所有变量(temp、counter 等)在类型之前出现错误,并尝试在 while 循环中使用它们中的任何一个会导致“未声明的标识符”错误。我确保 bool 是通过做定义的

#ifndef bool
    #define bool char
    #define false ((bool)0)
    #define true ((bool)1)
#endif

在 .c 文件的顶部。我在 Stack Overflow 上搜索了答案,有人说旧的 C 编译器不允许你在同一个 block 中声明和初始化变量,但我不认为这是问题所在,因为当我注释掉这些行时

m->columns = numColumns(s);
m->rows = numRows(s);
m->data = (double*)malloc(sizeof(double) * (m->rows * m->columns));

所有语法错误都消失了,我不知道为什么。感谢您的帮助。

---编辑---- 请求 Matrix 的代码

typedef struct {
    int rows;
    int columns;
    double *data;
}Matrix;

最佳答案

在不兼容 C99 的 C 编译器中(即 Microsoft Visual C++ 2010)(感谢 Mgetz 指出这一点),您不能在 block 中间声明变量。

因此尝试将变量声明移动到 block 的顶部:

void LoadValues(char *s, Matrix *m){
    int counter = 0;
    double temp;
    bool decimal;
    int numDec;
    m->columns = numColumns(s);
    m->rows = numRows(s);
    m->data = (double*)malloc(sizeof(double) * (m->rows * m->columns));
    while(*s != '\0'){
        .
        .
        .
    }
}

关于C语法错误: missing ';' before 'type' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18083624/

相关文章:

visual-c++ - 如何修复 Visual C++ 2010 中静态库项目中的警告 MSB8012?

c++ - 指针输出让我困惑

c 函数返回失去精度

sql - 语法错误: expected name_wo_function_call

javascript - 如何修复此语法错误?

c++ - Visual C++ 2010 fatal error C1083;没有权限

visual-c++-2010 - 如何将外部静态库添加到 Visual C++ 2010 Express 项目

c - 微 Controller PIC16F877A c编码,同时控制占用传感器和光电二极管

c - 了解 int 变量如何在 C 中存储值

python - 语法错误 : invalid syntax in conda