Syntaxfehler: fehlendes ‘;’ vor ‘typ’

Lesezeit: 3 Minuten

Benutzer-Avatar
Kosmo D

Also ich habe diesen Fehler:

Fehler 3 Fehler C2143: Syntaxfehler: fehlendes ‘;’ vor ‘type’ g:\lel\tommy\tommy\tommy.c 34 tommy

aus diesem Codeblock:

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

Ich habe keine Ahnung, was das bedeutet, oder wo ich den Fehler mache. Bitte helfen Sie.

AKTUALISIEREN:

Während die ursprüngliche Frage gelöst wurde, habe ich genau den gleichen Fehler erhalten, jedoch in einem anderen Codeblock, und ich schreibe gemäß der Empfehlung der gewählten Antwort:

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

Ich bin hier wirklich verloren, ich lese diesen Code und habe keine Ahnung, warum er mir den gleichen Fehler gibt.

  • Nur eine kurze Anmerkung zu den anderen Antworten: Sie haben “cols” innerhalb der Struktur deklariert, verwenden jedoch “columns” in der verschachtelten Schleife

    – John

    11. November 2012 um 22:41 Uhr

Benutzer-Avatar
Michael Burr

Beim Kompilieren eines C-Programms erlaubt MSVC nicht, dass Deklarationen Anweisungen in einem Block folgen (es verwendet alte C90-Regeln – Unterstützung für Deklarationen gemischt mit Anweisungen wurde C im Standard von 1999 hinzugefügt).

Verschieben Sie die Erklärung von double *ptr an die Spitze von 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

    // ...
}

Ich wünschte wirklich, MS würde diese “Erweiterung” für ihren C-Compiler implementieren.

  • Microsoft hat dies im Plattform-Toolset v120 implementiert.

    – Schlupf

    27. Juni 2014 um 19:47 Uhr

  • Ja – zu einer angenehmen Überraschung hat MS dem C-Compiler C99-Features hinzugefügt. Platform Toolset v120 entspricht VS 2013 (cl.exe v18.00), wenn ich mich nicht irre.

    – Michael Burr

    27. Juni 2014 um 20:15 Uhr

Kompilierst du mit c99 oder c89?

Der Fehler scheint darauf zurückzuführen zu sein, dass Sie eine Variable im Hauptteil der Funktion definieren (zulässig in c99, nicht in c89). Bewegen double *ptr an den Anfang der Funktion und dann einfach zuweisen ptr = mat->matrix; wo jetzt der fehler liegt.

1364280cookie-checkSyntaxfehler: fehlendes ‘;’ vor ‘typ’

This website is using cookies to improve the user-friendliness. You agree by using the website further.

Privacy policy