Accessing a 2D char array via a pointer in C

Hello guys. I have a question about the pointer to a 2D char array.
I have a 2D char array

char david[10000][100];

some items were inserted and there are still some spaces left
These spaces should be filled in inside a function, called openMode
which means

1
2
3
4
5
6
7
8
9
10
11
12
int main()
{
   char david[10000][100];
   /* some items were inserted here.
   e.g. david[0] = "hello";
   */
   openMode(xxx);
}
void openMode(xxx)
{
   /*operation......*/
}


Therefore, I have to pass the address of david as a parameter to openMode, but I have no idea how to do it, what those xxx should be?

Thanks.

Last edited on
closed account (S6k9GNh0)
1
2
3
4
5
6
7
8
9
int main()
{
   char david[10000][100];
   openMode(&david);
}
void openMode(char ** david)
{
   /*operation......*/
}


Can't test it now but I think that's how it works...I remember having to do this with some cheesy grid I made once...
Last edited on
multi-dimentional arrays are ugly. I avoid them for this very reason.

computerquip's example works for a dynamically allocated 2D array (ie: a pointer to a pointer), but does not work with "normal" 2D arrays. To pass a multidimensional array, you need to know the size of every dimension except the first one:

1
2
3
4
5
6
7
8
9
10
11
12
13
void openMode(char blah[][100])  // need the [100] here
{
}

int main()
{
  char david[10000][100];  // sidenote:  probably a bad
                               // idea to put this much on the stack

  openMode( david );

  return 0;
}
Last edited on
closed account (S6k9GNh0)
It was how to pass a 2D array via a pointer.
Correct.

My example does that. Changes made to 'blah' in openMode will change the given 'david' pointer in my example.

I don't know how you'd pass an array by value -- if you even can.
hey men, take it easy=]
thanks Disch, by using your method, I can successfully change the given 'david' pointer in function openMode.
thanks all of you.

&david;

is equivalent to char ***david!!!

it will make it triple pointer. computerquip can use it his way.. but then he needs to pass the size of the array also.

like;
openMode(david, 1000, 100); //dont pass the address, which makes it triple pointer. ;)

Topic archived. No new replies allowed.