簡體   English   中英

C語法錯誤:缺少';' 在“類型”之前

[英]C syntax error : missing ';' before 'type'

我在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'){
        .
        .
        .
    }
}

當我嘗試構建解決方案時,我遇到了“缺失”; 我所有變量(溫度,計數器等)的“ before type”錯誤,嘗試在while循環中使用其中任何一個都會導致“未聲明的標識符”錯誤。 我確保通過定義bool

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

在.c文件的頂部。 我已經在Stack Overflow上搜索了答案,有人說舊的C編譯器不允許您在同一塊中聲明和初始化變量,但是我不認為這是問題所在,因為當我注釋掉這些行時

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

所有語法錯誤都消失了,我也不知道為什么。 任何幫助表示贊賞。

---編輯----要求矩陣代碼

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

在不兼容C99的C編譯器(即Microsoft Visual C ++ 2010)中(感謝Mgetz指出這一點),您不能在塊中間聲明變量。

因此,嘗試將變量聲明移動到塊的頂部:

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'){
        .
        .
        .
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM