Per*_*lah 2 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'){
.
.
.
}
}
Run Code Online (Sandbox Code Playgroud)
当我尝试构建解决方案时,我得到了"缺失"; 在为所有变量(temp,counter等)输入"error"之前,并尝试在while循环中使用它们中的任何一个会导致"未声明的标识符"错误.我确保通过这样做来定义bool
#ifndef bool
#define bool char
#define false ((bool)0)
#define true ((bool)1)
#endif
Run Code Online (Sandbox Code Playgroud)
在.c文件的顶部.我搜索了Stack Overflow的答案,有人说旧的C编译器不允许你在同一个块中声明和初始化变量,但我认为这不是问题,因为当我注释掉这些行时
m->columns = numColumns(s);
m->rows = numRows(s);
m->data = (double*)malloc(sizeof(double) * (m->rows * m->columns));
Run Code Online (Sandbox Code Playgroud)
所有的语法错误消失了,我不明白为什么.任何帮助表示赞赏.
---编辑----请求Matrix的代码
typedef struct {
int rows;
int columns;
double *data;
}Matrix;
Run Code Online (Sandbox Code Playgroud)
在不符合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'){
.
.
.
}
}
Run Code Online (Sandbox Code Playgroud)