Passing 2 dimensional arrays to functions

This is a discussion on Passing 2 dimensional arrays to functions within the C++ Programming forums, part of the General Programming Boards category; What is the syntax for having a function accept a 2D array as a parameter without the function knowing how ...

  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,702
    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
    32,852
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >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,702
    >> 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,796
    >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,796
    >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, 12:31 PM
  2. Replies: 12
    Last Post: 06-06-2008, 06:26 PM
  3. Replies: 7
    Last Post: 04-19-2006, 12:17 PM
  4. Help Understanding Passing Arrays To Functions
    By jrahhali in forum C++ Programming
    Replies: 7
    Last Post: 04-10-2004, 03:57 PM
  5. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 06:58 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21