所以我有這樣的錯誤:語法錯誤:缺少';'之前「類型」
Error 3 error C2143: syntax error : missing ';' before 'type' g:\lel\tommy\tommy\tommy.c 34 tommy
從這個代碼塊:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <malloc.h>
#include <conio.h>
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 < n; j++)
{
double x;
if (fscanf(fptr, " %5.2lf", &x) != 1)
{
fprintf(stderr, "Failed to read element matrix[%d,%d]\n", i, j);
free(mat->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;
}
我不知道這意味着什麼,或者我正在做的錯誤。請幫忙。
UPDATE:
雖然原來的問題解決了,我已經收到了完全相同的錯誤,但在另一個代碼塊,而我正在寫按照建議的通過所選擇的答案:
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 < mat_left->rows; i++, pa += mat_left->cols)
for (j = 0; j < b->w; j++)
*p++ = dot(pa, mat_right->matrix + j, mat_left->cols, mat_right->cols);
return 0;
}
我真的迷失在這裏,我正在閱讀這段代碼,不知道爲什麼它給了我同樣的錯誤。
只需簡單的說明即可添加到其他答案中,您在結構中聲明瞭「cols」,但在嵌套循環中使用「列」 – John