Passing multidimensional arrays to functions

This is a discussion on Passing multidimensional arrays to functions within the C Programming forums, part of the General Programming Boards category; How can one create a function that will accept arbitrary length multidimensional arrays? eg, int foo[3][4]; int foo2[5][10]; int foo3[x][y]; ...

  1. #1
    Registered User
    Join Date
    Dec 2001
    Posts
    46

    Passing multidimensional arrays to functions

    How can one create a function that will accept arbitrary length multidimensional arrays?

    eg,

    int foo[3][4];
    int foo2[5][10];
    int foo3[x][y];

    int somefunction(???)
    {
    /*accept input in the form of any array and process accordingly.*/
    }

  2. #2
    Unregistered
    Guest
    int somefunction(int foo[][4]);
    or you can try
    int somefunction(int **foo);
    just to thoroughly test things out

    The compiler must know the size of the second and any subsequent dimensions when using array notation, and pointer notation can get confusing fast.

  3. #3
    Registered User
    Join Date
    Dec 2001
    Posts
    46
    I've already tried that. When you try

    int somefunction(**foo) and call

    somefunction(foo)

    you get an error "can't convert from int [3][4] to int **"

    Conversely, I'm sure the int somefunction(*foo[4]) would work perfectly for arrays with the second dimension being 4, but I'm trying to create a function with arbitrary input. Eg, you could call the same function with a 4x4 array, a 12x30 array, a 2x5 array, etc.

    Thanks for the help though,

    -Max

  4. #4
    Unregistered
    Guest
    What I was trying to get at before was I don't believe there is a way to pass a multi-dimensional array of any size to a function, the function has to know the lengths of the latter dimensions.

    Though you can have a multi-dimensional array that is the maximum size and pass that to the function, then have another array the same size but only fill what you need and have the rest as NULL. Then you can pass an array of any size to a function and have a good way to process it. This gets to be a problem when the arrays get very large though

    ex.
    int foo[20][10] = {0}; //gets filled completely
    int bar[20][10] = {0}; //gets filled half-way
    int baz[20][10] = {0}; //only has a few elements

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Manipulating Character arrays in functions.
    By kbro3 in forum C++ Programming
    Replies: 11
    Last Post: 08-16-2008, 03:24 AM
  2. Replies: 2
    Last Post: 07-03-2008, 12:31 PM
  3. Replies: 12
    Last Post: 06-06-2008, 06:26 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