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");
    }  
4
  • This declaration int matrix[][] is invalid. Commented Dec 24, 2019 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. Commented Dec 24, 2019 at 16:56
  • I see multiple declaration of left() here. Why this int left(); ? Remove it, as you are declaring inside one function. Commented Dec 24, 2019 at 16:56
  • 1
    There are lots of problems here. Braces aren't balanced, you have a function defined inside another function. Commented Dec 24, 2019 at 17:00

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.