Thread: multidimensional arrays as arguments

  1. #1
    Registered User
    Join Date
    Dec 2003
    Posts
    2

    Unhappy multidimensional arrays as arguments

    This is a really basic question, but i can't find the answer ANYWHERE. Basically, how do you pass a multidimensional array as an argument?

    Code:
    //this works:
    won(ar[])
    {
    }
    
    //this does not work
    won(ar[][])
    {
    }

  2. #2
    The Defective GRAPE Lurker's Avatar
    Join Date
    Feb 2003
    Posts
    949
    Pointers.
    Do not make direct eye contact with me.

  3. #3
    Registered User
    Join Date
    Dec 2003
    Posts
    2
    thx

  4. #4
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Give everything types: return type, parameter type.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    If you have
    int array[2][3][4];

    You declare and define the function with the following parameter
    void myfunc ( int array[2][3][4] );
    Quite simply, you just copy/paste the declaration of the array you want to pass to the function.

    And you call this function like so
    myfunc( array );

    > //this does not work
    > won(ar[][])
    You are only allowed to leave the left-most dimension empty. All the others must be present.
    This is equivalent to the above example
    void myfunc ( int array[][3][4] );
    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.

  6. #6
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Correct me if I'm wrong but wouldn't passing a pointer to the array be better than passing the entire array?

    Or does the compiler then get a pointer to the start of the array on the stack and then simply use offsets from there.

    Now that I've thought about it, the only disadvantage of passing it via the stack is the cleanup that needs to be done on exiting the function.

    Perhaps execution within the function would not suffer at all.

  7. #7
    ‡ †hë Ö†hÈr sîÐè ‡ Nor's Avatar
    Join Date
    Nov 2001
    Posts
    299
    it makes a copy of the array on the stack. yes pointers would be better.
    Try to help all less knowledgeable than yourself, within
    the limits provided by time, complexity and tolerance.
    - Nor

  8. #8
    Registered User DCII764II00's Avatar
    Join Date
    Dec 2003
    Posts
    7

    Hmm~
    I did not know you could pass arrarys through functions like that.. I thought only pointers to array elements would be more efficient and a lot more safer than passing an entire array.. Well I didn't even know the idea of passing an array existed.. Wouldn't that generate some sort of error?..

    Farewell~
    DCII764II00

  9. #9
    ‡ †hë Ö†hÈr sîÐè ‡ Nor's Avatar
    Join Date
    Nov 2001
    Posts
    299
    when you call a functions like

    DoWhatIWant(int this_long);

    Code:
        DoWhatIWant(172);
    what happens is the program makes a copy of this_long and places it on the stack.
    one copy is what you used to call your function and the other is what the function is using. if you make any changes to this_long with in the function when it exits/return the copy your functions was using poped off the stack.

    t things arize. one is Scope
    the other is a problem with memory. if you have 512 megs of ram and you pass an array[300megs big] and not a pointer to it then the compiler will make a copy and the function will use it.
    now some math
    (300+300=600) (512-600 = [b]memory error[/red])

    and the process of copying the varaible to the stack takes some time.

    now if you pass a pointer to your array your compiler will use the memory at the address the pointer points to... something like that anyways it saves the cpu the trouple of having to copy a VERY large amount of data.

    DoWhatIWant(int* this_long);
    Try to help all less knowledgeable than yourself, within
    the limits provided by time, complexity and tolerance.
    - Nor

  10. #10
    Tha 1 Sick RAT
    Join Date
    Dec 2003
    Posts
    271
    You can use pointers but i think if you want to do it that way then you need to pass it with range of the 2nd subscript so given the following:
    Code:
    int array[5][7];
    //define the array thus: 
    "return type" arrayfunc([5][7]);  //1st subscript optional me thinks
    //then call your function like so: 
     arrayfunc(array);
    Hope that helps.

  11. #11
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    now if you pass a pointer to your array your compiler will use the memory at the address the pointer points to... something like that anyways it saves the cpu the trouple of having to copy a VERY large amount of data.
    And you are guaranteed with a pointer that has already been correctly initialized that if you return a reference to what it points to...you will never be pointing to invalid memory.

    Making a copy of the array if and when you change it will most certainly eat cycles although I can think of several ways in assembly that this could be worked around - but it will still take more cycles than just passing a pointer to an array.

  12. #12
    Registered User
    Join Date
    Sep 2003
    Posts
    135
    When you pass an array as a function parameter there is a conversion to a pointer to the first element of the array. So, when you pass an array you don't pass the array elements by value, you pass a pointer to the first element of the array.

  13. #13
    Registered User
    Join Date
    Oct 2003
    Posts
    7

    Red face

    well it's true when youre passing an array to a function you're really passing a pointer to the array, it doesn't make a copy of the array

    http://www.cplusplus.com/doc/tutorial/tut3-1.html

  14. #14
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    > it makes a copy of the array on the stack. yes pointers would be better.
    No it doesn't!
    All arrays are passed as "pointers to first element" of the array, no matter how you declare the function prototype.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multidimensional Arrays
    By jordanguyoflove in forum C Programming
    Replies: 4
    Last Post: 10-16-2008, 06:16 PM
  2. Arrays as function arguments :(
    By strokebow in forum C Programming
    Replies: 10
    Last Post: 11-18-2006, 03:26 PM
  3. dynamic multidimensional arrays as function arguments
    By magda_k in forum C++ Programming
    Replies: 1
    Last Post: 03-20-2006, 04:00 PM
  4. Multidimensional Arrays?
    By CreatedByShadow in forum C++ Programming
    Replies: 7
    Last Post: 01-13-2006, 10:35 PM
  5. Pointers to Multidimensional Arrays
    By kidburla in forum C Programming
    Replies: 10
    Last Post: 10-29-2005, 10:45 PM