Tyler McAnally - 2 months ago 9
C Question

# 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
--------------------------------------------------------------------------------
|                                                                              |
|                                                                              |
|                                                                              |
|                                                                              |
|                                                                              |
|                                                                              |
|                                                                              |
|                                                                              |
|                                                                              |
|                                                                              |
|                                                                              |
|                                                                              |
|                                                                              |
|                                                                              |
|                                                                              |
|                                                                              |
|                                                                              |
|                                                                              |
|                                                                              |
--------------------------------------------------------------------------------
``````

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.*
*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).