简体   繁体   中英

subscripted value is neither array nor pointer nor vector in C language

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");
    }  

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM