Reputation: 19
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;
Upvotes: 2
Views: 1812
Reputation: 55609
In C compilers not compliant with C99 (i.e. 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'){
.
.
.
}
}
Upvotes: 7