passing a pointer to a 2D array to a function

X

xtheunknown0

I want to be able to pass a pointer to a 2D array of chars to a function
such that the function can access the array like this:

grid[j] = 'a';

I can do a similar thing with 1D arrays (of any data type), but I'm lost
with 2D arrays. So far, I've written a program that doesn't compile. Could
you please help?

#include <stdio.h>

#define MAX 500

void foo(char *grid[]) {
}

int main(void) {
char grid[MAX][MAX];
foo(grid);
return 0;
}

TIA,
xtheunknown0
 
B

Ben Bacarisse

I want to be able to pass a pointer to a 2D array of chars to a function
such that the function can access the array like this:

grid[j] = 'a';

I can do a similar thing with 1D arrays (of any data type), but I'm lost
with 2D arrays. So far, I've written a program that doesn't compile. Could
you please help?


Start with this: http://c-faq.com/aryptr/pass2dary.html Do also read the
linked sections as well.

You will probably have further questions, but they will be from a more
informed starting point.

<snip>
 
O

osmium

I want to be able to pass a pointer to a 2D array of chars to a function
such that the function can access the array like this:

grid[j] = 'a';

I can do a similar thing with 1D arrays (of any data type), but I'm lost
with 2D arrays. So far, I've written a program that doesn't compile. Could
you please help?

#include <stdio.h>

#define MAX 500

void foo(char *grid[]) {


You have probably solved your problem by now. But note that you can detect
the problem in the line above. Only one dimension of an array in C is
"free", the programmer must provide the other one and give it to the called
function, either via a parameter or a global value. Globals are frowned on,
of course. So you start by putting a number in the brackets.

"Free" as used above, simply means that the (one dimensional) array dribbles
off to infinity, which can be a source of endless amusement.
 
B

Barry Schwarz

I want to be able to pass a pointer to a 2D array of chars to a function
such that the function can access the array like this:

grid[j] = 'a';

I can do a similar thing with 1D arrays (of any data type), but I'm lost
with 2D arrays. So far, I've written a program that doesn't compile. Could
you please help?

#include <stdio.h>

#define MAX 500

void foo(char *grid[]) {
}

int main(void) {
char grid[MAX][MAX];
foo(grid);
return 0;
}


What is wrong with doing it the easy way?
void foo(char x[MAX][MAX]);
or the equivalent
void foo(char x[][Max]);

Even though the parameter looks like a 2d array, its actual type is
pointer to an array of MAX char
or expressed syntactically as
char (*x)[MAX]
and you could substitute this for the parameter text above.
 
F

Fred

osmium" ... said:
<snip>
#include <stdio.h>
#define MAX 500
void foo(char *grid[]) {

You have probably solved your problem by now.  But note that you can detect
the problem in the line above.  Only one dimension of an array in C is
"free", the programmer must provide the other one and give it to the called
function, either via a parameter or a global value.  Globals are frowned on,
of course.  So you start by putting a number in the brackets.

"Free" as used above, simply means that the (one dimensional) array dribbles
off to infinity, which can be a source of endless amusement.

Thank you for this information and thank you to pete.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top