【问题标题】:Syntax error : missing ';' before 'type'语法错误:缺少';'在“类型”之前
【发布时间】:2012-11-11 22:33:19
【问题描述】:

所以我有这个错误:

错误 3 错误 C2143:语法错误:缺少 ';'前 '类型' g:\lel\tommy\tommy\tommy.c 34 汤米

来自这段代码:

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

我真的迷路了,我正在阅读这段代码,但不知道为什么它会给我同样的错误。

【问题讨论】:

  • 只是添加到其他答案的快速注释,您在结构中声明了“cols”,但在嵌套循环中使用了“columns”

标签: c


【解决方案1】:

在编译 C 程序时,MSVC 不允许声明跟在块中的语句之后(它使用旧的 C90 规则 - 在 1999 年的标准中添加了对与语句混合的声明的支持)。

将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 编译器中实现这个“扩展”。

【讨论】:

  • 微软已经在平台工具集 v120 中实现了这一点。
  • 是的 - 令人惊喜的是,MS 一直在向 C 编译器添加 C99 功能。如果我没记错的话,Platform Toolset v120 对应于 VS 2013 (cl.exe v18.00)。
【解决方案2】:

你是用 c99 还是 c89 编译的?

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-15
    相关资源
    最近更新 更多