Alex Rivera | Logout

Syntax error : missing ';' before 'type'

Asked 2012-11-11T22:33:19.053
27

So i have this error:

Error 3 error C2143: syntax error : missing ';' before 'type' g:\lel\tommy\tommy\tommy.c 34 tommy

from this block of code:

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

I have no idea what that means, or where I'm making the mistake. Please help.

UPDATE:

While the original question was solved, i have received the exact same error, but in another block of code, and I'm writing as per recommended by the cho

c
Edit
Report

1 Answer

42

When compiling a C program, MSVC doesn't allow declarations to follow statements in a block (it uses old C90 rules - support for declarations mixed with statements was added to C in the 1999 standard).

Move the declaration of double *ptr to the top of 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

    // ...
}

I really wish MS would implement this 'extension' to their C compiler.

answered 2012-11-11T22:36:51.313

Your Answer