Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   passing a pointer to a 2D array to a function (http://www.velocityreviews.com/forums/t740468-passing-a-pointer-to-a-2d-array-to-a-function.html)

xtheunknown0@gmail.com 12-20-2010 09:39 AM

passing a pointer to a 2D array to a function
 
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[i][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


Ben Bacarisse 12-20-2010 01:59 PM

Re: passing a pointer to a 2D array to a function
 
xtheunknown0@gmail.com writes:

> 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[i][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>
--
Ben.

osmium 12-20-2010 04:31 PM

Re: passing a pointer to a 2D array to a function
 
<xtheunknown0@gmail.com> wrote:

>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[i][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.



Barry Schwarz 12-21-2010 05:07 PM

Re: passing a pointer to a 2D array to a function
 
On Mon, 20 Dec 2010 20:39:59 +1100, xtheunknown0@gmail.com wrote:

>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[i][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.

--
Remove del for email

Fred 01-10-2011 09:33 AM

Re: passing a pointer to a 2D array to a function
 
"osmium" ... wrote:
> <xtheunkno...@gmail.com> wrote:
> > <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.


All times are GMT. The time now is 05:44 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.