0

I have an issue. I want to use the size defined by user in the matrix, but I have that error...

Any idea?

I'm using the C Language

int play(int matrix[][],int sz, char play_user[1]){
    if (play_user[0] =='a'){
        int left();
        left(matrix[sz][sz],sz);
    }


int left(int matrix[][], int sz){
    for(int i=0;i<sz;++i){                    
        for(int j=0;j<sz;++j){
            printf("%d\t", matrix[i][j]);
        }
        printf("\n");
    }  
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
stuck
  • 13
  • 3
  • This declaration int matrix[][] is invalid. – Vlad from Moscow Dec 24 '19 at 16:56
  • 3
    `int matrix[][]` isn't legal C. And when next posting a question about an error, include the full, verbatim error message in your question, *and* mark the line on which it occurs in your source list. – WhozCraig Dec 24 '19 at 16:56
  • I see multiple declaration of `left()` here. Why this `int left();` ? Remove it, as you are declaring inside one function. – Achal Dec 24 '19 at 16:56
  • 1
    There are lots of problems here. Braces aren't balanced, you have a function defined inside another function. – Barmar Dec 24 '19 at 17:00

1 Answers1

1

Rather than

int left(int matrix[][], int sz);

Pass the size information first. This uses a variable length array, available in C99, optional in C11, C17.

int left(int sz, int matrix[sz][sz])

Or better yet, pass both dimensions and use size_t

int left(size_t rows, size_t cols, int matrix[rows][cols]);

int play(size_t sz, int matrix[sz][sz], char play_user[1]) {
  if (play_user[0] == 'a') {
    return left(sz, sz, matrix);
  }
  return 0;
}

int left(size_t rows, size_t cols, int matrix[rows][cols]) {
  for (size_t r = 0; r < rows; ++r) {
    for (size_t c = 0; c < cols; ++c) {
      printf("%d\t", matrix[r][c]);
    }
    printf("\n");
  }
  return 0;
}

See also Passing a multidimensional variable length array to a function

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256