Reading matrices of unkown dimension : <p>I'm trying to read a matrix from a file formatted in this way</p>

<pre>
1 2 3 4
4 5 6 7
8 9 10 11
*
1 0 1
0 1 1
1 1 1]
</pre>

<p>but I don't know how to pass the multidimensional array with its dimension given by rows[] and cols[] arrays to the function read_matrix().</p>

<p>I am able to get the correct dimension of the two matrices</p>

<pre><code>#include &lt;stdio.h&gt;
#include &lt;string.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;ctype.h&gt;

int get_dim(char *file, int *r, int *col);
/*gets the dimension and type of operation*/

void read_matrix(char *file, int (*matA)[], int(*matB)[], int *m, int *n);

int main (int argc, char *argv[]){

    int i,j;
    int rows[2]= {0,1}; /* The last line of the file does not contain '\n' */
    int cols[2]= {0,0};
    int operation; /*type of operation 1 for * and 2 for + */

    operation = get_dim(argv[1], rows, cols);

    int A[rows[0]][cols[0]];
    int B[rows[1]][cols[1]];

    printf("A is a matrix %d x %d\n",rows[0], cols[0]);

    if(operation != 0)
        printf("B is a matrix %d x %d\n", rows[1], cols[1]);

    read_matrix(argv[1], A, B, rows, cols);

    /*printing the matrices */

    for(i = 0; i &lt; rows[0]; i++){
        for(j=0; j &lt; cols[0]; j++){
            printf("%d ", A[i][j]);
        }
        printf("\n");
    }

    printf("*\n");

     for(i = 0; i &lt; rows[1]; i++){
        for(j=0; j&lt; cols[1]; j++){
            printf("%2d ", B[i][j]);
        }
        printf("\n");
    }    
    return 0;
}

int get_dim(char *file, int *r, int *col){

   FILE *fp; 
   int c;
   int op=0;
   int n =0; /*to check all coefficients in a row */

   /* opening file for reading */
   fp = fopen(file, "r");

   if(fp == NULL) {
      perror("Error opening file");
   }

   while ( (c = getc(fp)) != ']')
   {       
       if(isdigit(c) &amp;&amp; n==0){
           if(op == 0)
               col[0]++;
           else
               col[1]++;
       }        

    //Count whenever new line is encountered
    if (c == '\n'){
        n=1;
        if(op == 0)
            r[0]++;
        else 
            r[1]++;
    }     

        if(c == '*'){
            op=1;
            n =0;
            c = getc(fp);
        }
        else if(c == '+'){
            op=2;
            c = getc(fp);
        }

        //take next character from file.
    }
    fclose(fp);
    return op;
}

void read_matrix(char *file, int (*matA)[], int(*matB)[], int *m, int *n){

    int i,j;
    FILE *fp;

    if( (fp = fopen(file, "r")) != NULL ){

        for(i=0; i &lt; m[0]; i++)
            for(j=0; j &lt; n[0]; j++)
                fscanf(fp, "%d", &amp;matA[i][j]);    

        /*skip the line containing the character operator */
        while ( fgetc(fp) != '\n')
            fgetc(fp);        

        for(i=0; i &lt; m[1]; i++)
            for(j=0; j &lt; n[1]; j++)
                fscanf(fp, "%d", &amp;matB[i][j]);    
    }

    fclose(fp);
}
</code></pre>

<p>I should define the pointers this way: int(*matA)[cols[0]] and int(*matB)[cols[1]].
In read_matrix() by declaring this pointer: int(*matB)[] I get the error : invalid use of array with unspecified bounds, that I understand. But the bound is determined by the get_dim() function.</p>
