Thread: Parse error on line 28 and 36

  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    39

    Parse error on line 28 and 36

    Hi guys, I'm trying to learn how to read and print a matrix using C. I wrote the following code. However, the compiler gave me this error.

    tutorial_10_qn3_tryagain.c: In function `readMatrix':
    tutorial_10_qn3_tryagain.c:28: error: parse error before '(' token
    tutorial_10_qn3_tryagain.c: In function `printMatrix':
    tutorial_10_qn3_tryagain.c:36: error: parse error before '(' token

    I'm not sure what's wrong. I counted the brackets and made sure I did not miss out any, neither did i miss out ';'. Can you please advise what's wrong. thanks.

    Code:
      #include <stdio.h>
     
     struct Matrix
     {
        int rows; int columns;
         int mat [10][10];
     };
     
     void readMatrix (struct Matrix *M);
     void printMatrix (struct Matrix M);
     
     int main ( )
     {
          struct Matrix M ={0,0,{{0}}}, *ptr;
         ptr= &M;
         readMatrix(&M);
         printMatrix(M);
     
         return 0;
     }
     
     void readMatrix ( struct Matrix *M)
     {
         int i=0; int j=0;
         scanf("%d %d", &((*M).rows), &((*M).columns));
         for ( i=0; i<((*M).rows); i++)
             for (j=0; j<((*M).columns); j++)
                 scanf("%d",& ((*M).(mat[i][j])));
     }
     void printMatrix (struct Matrix M)
     {
         int i=0; int j=0;
         for (i=0; i<(M.rows); i++)
            {
                for (j=0; j<(M.columns); j++)
                 printf("%d", (M.(mat[i][j])));
               printf("\n");
              }
      }

  2. #2
    Registered User
    Join Date
    Dec 2011
    Posts
    39
    Oh...sorry guys...i got it....i realised when i removed all the brackets and merely type these...it works....hmmm....is the brackets obstructing something?

    Code:
    scanf("%d",&(*M).mat[i][j]);
    printf("%d",M.mat[i][j] );

  3. #3
    Registered User
    Join Date
    Dec 2011
    Posts
    39
    Sorry guys, could u teach me the following?

    1) How do i print the brackets properly for my matrix in c such that the output is like that?http://upload.wikimedia.org/wikipedi...fc41990150.png

    Currently the code i edited (below) only allows the matrix to be printed like that.

    [ 1 1 2
    2 4 -3
    ]


    Can you teach me how to print the brackets properly?

    Code:
      1 #include <stdio.h>
      2 
      3 struct Matrix
      4 {
      5     int rows; int columns;
      6     int mat [10][10];
      7 };
      8 
      9 void readMatrix (struct Matrix *M);
     10 void printMatrix (struct Matrix M);
     11 
     12 int main ( )
     13 {
     14     struct Matrix M ={0,0,{{0}}}, *ptr;
     15     ptr= &M;
     16     readMatrix(&M);
     17     printMatrix(M);
     18 
     19     return 0;
     20 }
     21 
     22 void readMatrix ( struct Matrix *M)
     23 {
     24     int i=0; int j=0;
     25     scanf("%d %d", &((*M).rows), &((*M).columns));
     26     for ( i=0; i<((*M).rows); i++)
     27         for (j=0; j<((*M).columns); j++)
     28             scanf("%d",&(*M).mat[i][j]);
     29 }
     30 void printMatrix (struct Matrix M)
     31 {
     32     int i=0; int j=0;
     33     printf("[");
     34     for (i=0; i<(M.rows); i++)
     35        {
     36            for (j=0; j<(M.columns); j++)
     37             printf("%d  ",M.mat[i][j] );
     38           printf("\n");
     39         }
     40     printf("]");
     41 }
    2) I'm puzzled to why adding some brackets creates errors. The code of the previous post was fine but i realised when i add brackets so that the code becomes like that (below), the compiler give me parse error. Can you please explain to me why is this so?

    Code:
    printf("%d", ((M).(mat[i][j])));
    Please advise. Thanks

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by jollykiddo View Post
    Oh...sorry guys...i got it....i realised when i removed all the brackets and merely type these...it works....hmmm....is the brackets obstructing something?
    Code:
    scanf("%d",&(*M).mat[i][j]);
    printf("%d",M.mat[i][j] );
    
    
    //            Try this:
    
    scanf("%d",&M.mat[i][j]);
    printf("%d",M.mat[i][j] );
    In this part of the for loop, you have one too many '(' (left parenthesis):
    Code:
    j<((*M).columns);
    You'll have far fewer errors if you use index/indices, instead of pointers, when working with arrays.
    Last edited by Adak; 12-18-2011 at 01:17 AM.

  5. #5
    Registered User
    Join Date
    Dec 2011
    Posts
    39
    okie. thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. parse cmd-line
    By quantt in forum C Programming
    Replies: 22
    Last Post: 01-24-2009, 05:02 AM
  2. Parse line using strtol
    By mike_g in forum C++ Programming
    Replies: 3
    Last Post: 10-05-2007, 09:47 AM
  3. keep getting a parse error
    By v3dant in forum C++ Programming
    Replies: 3
    Last Post: 09-09-2004, 01:39 AM
  4. Parse error
    By Andystudent in forum C Programming
    Replies: 3
    Last Post: 12-01-2003, 09:31 AM
  5. parse error before else
    By sdchem in forum C++ Programming
    Replies: 3
    Last Post: 04-10-2003, 01:31 PM