Thread: Passing 2 dimensional arrays to functions

  1. #1
    C/C++ homeyg's Avatar
    Join Date
    Nov 2004
    Location
    Louisiana, USA
    Posts
    209

    Passing 2 dimensional arrays to functions

    What is the syntax for having a function accept a 2D array as a parameter without the function knowing how large the array that is passed to it will be? Or if that's not possible, how in the world do you just pass a 2D array to a function? None of my books really deal with 2D arrays that much. Thanks in advance

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Code:
    void foo(int **two_d);
    gg

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Of course it depends on how you declared the array in the first place - like whether it's a true 2D array or whether its some combination of pointers looking a bit like an array.

    > without the function knowing how large the array that is passed to it will be?
    You can do this so long as the minor dimension does not change
    char foo[10][20];
    char bar[100][20];
    can be passed to the same function without any problems.

    The prototype for such a function being
    void func ( char (*a)[20] );

    func( foo );
    func( bar );
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >void foo(int **two_d);
    Bzzzt, wrong! That's not a two dimensional array. The following will not compile:
    Code:
    void foo(int **a);
    
    int main()
    {
      int array[5][5];
    
      foo(array);
    }
    The reason is because the whole "array name becomes a pointer" deal only works once per use, so the correct code would be:
    Code:
    void foo(int (*a)[5]);
    
    int main()
    {
      int array[5][5];
    
      foo(array);
    }
    Multidimensional arrays in C are tricky, so I usually recommend duplicating the original declaration in the parameter list:
    Code:
    void foo(int a[5][5]);
    
    int main()
    {
      int array[5][5];
    
      foo(array);
    }
    If you need a multidimensional array without the function knowing the size, you need to simulate a multidimensional array with pointers and memory allocation. Then your example will work, but only if the function's code has a way to get the size of all dimensions somehow.
    My best code is written with the delete key.

  5. #5
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    >> That's not a two dimensional array
    Not in your example, but it is in mine (as the parameter name clearly indicates).

    I shouldn't of made such a lazy, terse reply in the first place

    gg

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >but it is in mine (as the parameter name clearly indicates).
    You can call it whatever you want, but the type clearly indicates that the argument cannot be declared as a two dimensional array for the reasons that I described. If you want to think of dynamic memory made to look like an array as an actual array that's fine, it's very convenient and I do it myself. But when there are very real syntactic differences, you're expected to make the distinction.

    >I shouldn't of made such a lazy, terse reply in the first place
    My best code is written with the delete key.

  7. #7
    C/C++ homeyg's Avatar
    Join Date
    Nov 2004
    Location
    Louisiana, USA
    Posts
    209
    So, once I send the thing to the function,

    Code:
    int main()
    {
      int array[5][5];
    
      foo(array);
    }
    I can use it inside of the function like this:

    Code:
    void foo(int (*a)[5])
    {
         a[2][3]=6;
         cout<<a[2][3]<<endl;
    }
    Is this correct?

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Is this correct?
    Yes.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-03-2008, 11:31 AM
  2. Replies: 12
    Last Post: 06-06-2008, 05:26 PM
  3. Replies: 7
    Last Post: 04-19-2006, 11:17 AM
  4. Help Understanding Passing Arrays To Functions
    By jrahhali in forum C++ Programming
    Replies: 7
    Last Post: 04-10-2004, 02:57 PM
  5. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 06:58 PM