语法错误:缺少’;’ 在’类型’之前

所以我有这个错误:

错误3错误C2143:语法错误:缺少’;’ 在’type’之前g:\ lel \ tommy \ tommy \ tommy.c 34 tommy

来自这段代码:

#include  #include  #include  #include  #include  struct matrep { unsigned rows,cols; double *matrix; }; int matrix_read(struct matrep *mat, const char *filename) { FILE *fptr; unsigned m, n; if ((fptr = fopen(filename, "r")) == NULL) { fprintf(stderr, "Cannot Open File %s\n", "matrixA.txt"); return -1; } if (fscanf(fptr, "\n\nnrows %u, columns %u\n\n", &m, &n) != 2) { fprintf(stderr, "Failed to read dimensions\n"); return -1; } mat->matrix = (double *)malloc(sizeof(double) * m * n); if (mat->matrix == 0) { fprintf(stderr, "Failed to allocate %d*%d matrix\n", m, n); return -1; } double *ptr = mat->matrix;//this is where it says that the error occured. for (int i = 0; i < m; i++) { for (int j = 0; j matrix); mat->matrix = 0; mat->columns = 0; mat->rows = 0; return -1; } *ptr++ = x; } } fclose(fptr); mat->columns = m; mat->rows = n; return 0; // Success } int main(int argc, _TCHAR* argv[]) { return 0; } 

我不知道这意味着什么,或者我在犯错误的地方。 请帮忙。

更新:

虽然原始问题已经解决,但我收到了完全相同的错误,但是在另一个代码块中,我按照所选答案的建议编写:

 int matrix_multiplication(struct matrep *mat_left,struct matrep *mat_right,struct matrep *result) { if(mat_left->cols != mat_right->rows) { fprintf(stderr, "The number of columns from the left matrix are different from the number of colums from the right matrix"); return -1; } double *p = NULL;//this is where the same error occurs the first time double *pa = NULL; int i,j; result->rows = mat_left->rows; result->cols = mat_right->cols; p = result->matrix; for (pa = mat_left->matrix, i = 0; i rows; i++, pa += mat_left->cols) for (j = 0; j w; j++) *p++ = dot(pa, mat_right->matrix + j, mat_left->cols, mat_right->cols); return 0; } 

我真的迷失在这里,我正在阅读这段代码并且不知道它为什么会给我同样的错误。

在编译C程序时,MSVC不允许声明遵循块中的语句(它使用旧的C90规则 – 在1999标准中将对与语句混合的声明的支持添加到C中)。

double *ptr的声明移动到matrix_read()的顶部:

 int matrix_read(struct matrep *mat, const char *filename) { FILE *fptr; unsigned m, n; double *ptr = NULL; // ... ptr = mat->matrix; //this is where the error used to occur // ... } 

我真的希望MS能够为他们的C编译器实现这个’扩展’。

你在用c99或c89编译吗?

该错误似乎是因为您在函数体内定义了一个变量(在c99中允许而不是c89)。 将double *ptr移动到函数的开头,然后只分配ptr = mat->matrix; 现在的错误在哪里。