Thread: pass 3D array

  1. #1
    Registered User
    Join Date
    Jul 2005
    Posts
    3

    pass 3D array

    For 2D array, we can pass the array to other function like:
    Code:
    int array[5][10];
    ...
    func((int *)array, 5, 10);
    ...
    ...
    int func(int *array, int x, int y) {
        *(array + y * 0 + 8) = 1000;   /*change array[0][8]*/
        *(array + y * 1 + 8) = 2000;   /*change array[1][8]*/
    }
    I' m wondering how we can pass a 3D array...

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > For 2D array, we can pass the array to other function like:
    Since your attempt at 2D is wrong, 3D will be hopeless.

    If you have
    int array[5][10];

    The prototype for a function is
    void func ( int array[5][10] );

    A call to this function would be
    func ( array );

    And the implementation would be
    Code:
    void func ( int array[5][10] ) {
      array[0][0] = 0;
    }
    Notice how simply copy/pasting the array into the function parameter is all you need to do.
    Also notice the complete lack of any * or & operators anywhere in the code.

    Pedantically, you can reduce
    void func ( int array[5][10] );
    to
    void func ( int array[][10] );
    since C doesn't need to know the size of the first dimension of the array.
    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.

  3. #3
    Registered User
    Join Date
    Jul 2005
    Posts
    3
    What if we don't know exactly the size of array we gonna pass to func()?
    How can we create a func() which can accept any size of array?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    http://www.eskimo.com/~scs/C-faq/q6.19.html

    int a[5][10];
    int b[100][10];
    Can both be passed to void func ( int array[][10] );
    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. Pass struct array to function
    By aditya_t90 in forum C Programming
    Replies: 4
    Last Post: 03-30-2009, 11:54 AM
  2. How To pass 2 dimensional array of strings to a function
    By chottachatri in forum C Programming
    Replies: 15
    Last Post: 01-25-2008, 02:20 PM
  3. Need to pass a pointer of a unknown sized 2d array
    By (Slith++) in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2007, 10:15 PM
  4. Fast dynamic allocation of 3D array
    By Micko in forum C Programming
    Replies: 17
    Last Post: 07-22-2005, 07:10 AM
  5. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM