27

だから私はこのエラーがあります:

エラー 3 エラー C2143: 構文エラー: ';' がありません 「タイプ」の前 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;
}

それが何を意味するのか、どこで間違いを犯しているのか、私にはわかりません。助けてください。

アップデート:

元の質問は解決されましたが、まったく同じエラーを受け取りましたが、別のコードブロックで、選択した回答で推奨されているとおりに書いています:

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;
}

ここで本当に迷っています。このコードを読んでいて、同じエラーが発生する理由がわかりません。

4

2 に答える 2

42

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 コンパイラに実装してくれることを心から願っています。

于 2012-11-11T22:36:51.313 に答える
3

c99 または c89 でコンパイルしていますか?

エラーは、関数の本体内で変数を定義しているためと思われます (c89 ではなく c99 で許可されています)。関数の先頭に移動し、エラーが発生した場所double *ptrを割り当てます。ptr = mat->matrix;

于 2012-11-11T22:37:03.600 に答える