简体   繁体   中英

Pointer to 2D array with function

I'm having some trouble understanding how pointers work with two dimensional arrays. Even the error messages aren't helping me. I have a 2D array that I need a pointer for so I may manipulate it inside of functions (I think that's how you're supposed to do it anyway). Could someone please point out what I'm doing wrong and point me in the right direction?

Here's my code:

#include <stdio.h>
#include <time.h>

void init(char *array);

int main(int argc, char *argv[]) {
  char grid[21][80];
  char (*grid_ptr)[80];
  grid_ptr = grid;
  int i, j;
  init(*grid_ptr);

  for (i=0; i<21; i++) {
    for (j=0; j<80; j++) {
      printf("%c", grid_ptr[i][j]);
    }
    printf("\n");
  }

  return 0;
}

void init(char *array) {
  int i,j;
  for (i=0; i<21; i++) {
    for (j=0; j<80; j++) {
      *array[i][j] = ' ';
    }
  }
  for (i=0; i<21; i++) {
    *array[i][0] = '|';
    *array[i][79] = '|';
  }
  for (i=0; i<80; i++) {
    *array[0][i] = '-';
    *array[20][i] = '-';
  }
}

The errors are of this nature:

main.c:27:16: error: subscripted value is not an array, pointer, or vector
      *array[i][j] = ' ';

Allow me to say that you are making your life hard for no reason at all! :) You see when one wants to manipulate a 2D array, he can work on the array directly, by passing the array itself to the function, like this:

#include <stdio.h>
#include <time.h>

void init(int n, int m, char array[n][m]);

int main(int argc, char *argv[]) {
  const int n = 21, m = 80;
  char grid[n][m];
  int i, j;
  init(n, m, grid);

  for (i = 0; i < n; i++) {
    for (j = 0; j < m; j++) {
      printf("%c", grid[i][j]);
    }
    printf("\n");
  }

  return 0;
}

void init(int n, int m, char array[n][m]) {
  int i,j;
  for (i = 0; i < n; i++) {
    for (j = 0; j < m; j++) {
      array[i][j] = ' ';
    }
  }
  for (i = 0; i < n; i++) {
    array[i][0] = '|';
    array[i][m - 1] = '|';
  }
  for (i = 0; i < m; i++) {
    array[0][i] = '-';
    array[n - 1][i] = '-';
  }
}

which gives this lovely rectangle:

C02QT2UBFVH6-lm:~ gsamaras$ gcc -Wall main.c
C02QT2UBFVH6-lm:~ gsamaras$ ./a.out 
--------------------------------------------------------------------------------
|                                                                              |
|                                                                              |
|                                                                              |
|                                                                              |
|                                                                              |
|                                                                              |
|                                                                              |
|                                                                              |
|                                                                              |
|                                                                              |
|                                                                              |
|                                                                              |
|                                                                              |
|                                                                              |
|                                                                              |
|                                                                              |
|                                                                              |
|                                                                              |
|                                                                              |
--------------------------------------------------------------------------------

Now notice the changes I made to your code:

  1. I got rid of the pointer that refers to the array, since it's redundant.
  2. I passed the array itself as a parameter to the function, as described here .
  3. Instead of using magic numbers (21 and 80) all over the place, I declared two new constant variables, n and m , which are the dimension of your 2D array, n rows x m columns. *
  4. I use the dimensions to implement the very same logic you had implemented so far. Notice that I have to pass them as function parameters too.

As for the error, it implies that you are not accessing what you think that you are accessing! But let me not expand on this and keep it minimal here. :)


* Now if you want to change the dimensions of your 2D array, you just need to change n and/or m once, not everywhere in your code (which is bug prone).

In case anyone is using a C compiler that does not support the optional "variable length arrays" feature (eg the Microsoft compiler), here is another way:

void init( int rows, char (*array)[80] );

    // ... in main ...
    char grid[21][80];
    init(grid);          // use of grid_ptr is not required
    // ...

void init(int rows, char (*array)[80])
{
    int i,j;
    for (i = 0; i < rows; i++) {
      for (j = 0; j < 80; j++) {
        array[i][j] = ' ';

In the j loop you could replace 80 with sizeof *array . Or you could replace the entire j loop (in fact, both loops) with a call to memset .

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