Error while trying to initialize array /:

This is a discussion on Error while trying to initialize array /: within the C Programming forums, part of the General Programming Boards category; Hello, I have just started learning C as a course i'm taking in univ'. I need to make and manipulate ...

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    8

    Error while trying to initialize array /:

    Hello,

    I have just started learning C as a course i'm taking in univ'.

    I need to make and manipulate a matrix (I assumed by using two-dimensional array) but when I try to declare and initialize it gives me a compiler error: "error C2143: syntax error : missing ';' before 'type' "

    Now, I know what this error means but I have checked everything and tried to move the declaration around the code and it keeps giving me the same error.

    I'm posting part of the code (the user is supposed to input the matrix dimensions which I still have not thought about how to do)

    Code:
            {
                 int row;
                 int col;
                printf ("Please enter the matrix height ( number of rows ) :\n");
    
    
                scanf ("%d", &row);
                while (row < 2 || row > 15)
                {
                    printf ("Wrong input, the matrix height must be between 2 and 15 - try again :\n");
                    scanf ("%d", &row);
                }
                
    
    
                printf ("Please enter the matrix width ( number of columns ) :\n");
                scanf ("%d", &col);
                while (col < 2 || col > 15)
                {
                    printf ("Wrong input, the matrix width must be between 2 and 15 - try again :\n");
                    scanf ("%d", &col);
                }
                int mat [4][5];
    As you can see, the array is really not related to any of the code above and is in the correct syntax (I think) and still it gives me the error...

    I'll really appreciate any idea...

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,765
    Standard (C89) C doesn't allow you to mix declarations (such as your array) and statements.

    If your array is fixed dimensions, then move it to the start of the block.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Apr 2012
    Posts
    8
    thanks for the fast reply!

    The array dimensions are constant until the user wants to change them again (by inputting "row" and "col" again), so how can I get the dimensions from the user if the declaration is on top?

  4. #4
    Registered User
    Join Date
    Apr 2012
    Posts
    3
    As for as my knowledge,u can't initialize a variable of any datatype in any part of the program expect at the beginning.
    Please do correct me if am wrong .

    This should help ypu,

    Code:
    void main()
    {
    int row,col,mat[50][50],i,j;
    
    printf ("Please enter the matrix height ( number of rows ) :\n"); printf ("Please enter the matrix height ( number of rows ) :\n"); scanf ("%d", &row); while (row < 2 || row > 15) { printf ("Wrong input, the matrix height must be between 2 and 15 - try again :\n"); scanf ("%d", &row); } printf ("Please enter the matrix width ( number of columns ) :\n"); scanf ("%d", &col); while (col < 2 || col > 15) { printf ("Wrong input, the matrix width must be between 2 and 15 - try again :\n"); scanf ("%d", &col); } // Entering Elements inside the Matrix. for(i=0;i<row;i++) for(j=0;j<col;j++) scanf("%d",&mat[i][j]); // Printing the Elements inside the Matrix. for(i=0;i<row;i++) { for(j=0;j<col;j++) matrix("%d\t",mat[i][j]); printf("\n") ; } getch(); }
    ~FT
    Last edited by Abhishek Sp; 04-20-2012 at 12:04 PM.

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,765
    Typically, you do this
    int **mat;

    Then when you know the size
    Code:
    mat = malloc( row * sizeof(*mat) );
    for ( r = 0 ; r < row ; r++ ) {
      mat[r] = malloc( col * sizeof(*mat[r]) );
    }
    Later still, when you're done
    Code:
    for ( r = 0 ; r < row ; r++ ) {
      free(mat[r]);
    }
    free(mat);
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User
    Join Date
    Apr 2012
    Posts
    8
    Although I still cannot use malloc\free functions because it wasn't learnt in class yet, I declared the array on top of the code at maximum dimensions ([15][15]) and I only use the dimension the user inputs.

    It works perfectly (in the future i'll be able to use the more elegant functions...

  7. #7
    Registered User
    Join Date
    Apr 2012
    Posts
    44
    hehe good luck

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 10-18-2010, 03:55 AM
  2. Replies: 8
    Last Post: 09-15-2009, 04:14 AM
  3. initialize 2D array!
    By samirself in forum C Programming
    Replies: 1
    Last Post: 03-10-2005, 11:56 AM
  4. how to initialize a new array
    By Chiel in forum C++ Programming
    Replies: 4
    Last Post: 08-25-2003, 02:30 PM
  5. Initialize an array
    By cheesehead in forum C++ Programming
    Replies: 6
    Last Post: 12-12-2001, 04:28 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21