简体   繁体   中英

C syntax error : missing ';' before 'type'

I'm getting a very strange syntax error in a C project I'm doing in Microsoft Visual C++ 2010 Express. I have the following code:

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

When I try to Build Solution, I get the "missing ';' before type" error for all of my variables (temp, counter, etc), and trying to use any of them in the while loop causes an "undeclared identifier" error. I made sure bool was defined by doing

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

at the top of the .c file. I've searched Stack Overflow for answers and someone said that old C compilers don't let you declare and initialize variables in the same block, but I don't think that is the problem, because when I comment out the lines

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

all of the syntax errors go away and I have no idea why. Any help is appreciated.

---EDIT---- The code for Matrix was requested

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

In C compilers not compliant with C99 (ie Microsoft Visual C++ 2010) (thanks to Mgetz for pointing this out), you can't declare variables in the middle of a block.

So try moving the variable declarations to the top of the 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'){
        .
        .
        .
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM