passing a pointer to a 2D array to a function

Discussion in 'C Programming' started by xtheunknown0@gmail.com, Dec 20, 2010.

  1. Guest

    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
    , Dec 20, 2010
    #1
    1. Advertising

  2. 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[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.
    Ben Bacarisse, Dec 20, 2010
    #2
    1. Advertising

  3. osmium Guest

    <> 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[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.
    osmium, Dec 20, 2010
    #3
  4. On Mon, 20 Dec 2010 20:39:59 +1100, 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[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
    Barry Schwarz, Dec 21, 2010
    #4
  5. Fred Guest

    "osmium" ... wrote:
    > <> 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.
    Fred, Jan 10, 2011
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. James Vanns
    Replies:
    7
    Views:
    7,035
    Evan Carew
    Jan 21, 2004
  2. entitledX
    Replies:
    4
    Views:
    513
    Old Wolf
    Jan 12, 2006
  3. jimjim
    Replies:
    16
    Views:
    837
    Jordan Abel
    Mar 28, 2006
  4. Vijai Kalyan
    Replies:
    4
    Views:
    704
    Vijai Kalyan
    Nov 8, 2005
  5. PeterOut

    Passing a 2D array as a pointer to a pointer

    PeterOut, Aug 27, 2008, in forum: C Programming
    Replies:
    3
    Views:
    305
    Nick Keighley
    Aug 28, 2008
Loading...

Share This Page