Thread: using pointers to pass multi-d arrays

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    28

    using pointers to pass multi-d arrays

    hello, this is sort of related to my recent post, which was solved thanks to you guys.

    i had to take out the use of pointers.

    Question now is:

    "Is it possible to use pointers to pass multi-dimensional arrays through routines?"

    eg.
    CASE 1: This works
    - Passing the multi-d array as a non-pointer
    _______________________________
    int main( void )
    {

    byte set[10][15];

    func( set );

    }

    byte func( rule[][15] )
    {
    ...
    }
    _______________________________

    CASE 2: This doesn't work
    - Trying to pass the multi-d array as a pointer
    _______________________________
    int main( void )
    {

    byte set[10][15];

    func( set );

    }

    byte func( *rule[][15] ) // Notice the extra *
    {
    ...
    }
    _______________________________

    Is it possible to pass them through as a pointer?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Arrays are always passed as a pointer, so passing a different kind of pointer isn't a big improvement.

    Your 2nd example would be like this - but the extra level of indirection which you get doesn't buy you anything
    Code:
    byte func( byte (*rule)[10][15] );
    int main( void )
    {
        byte set[10][15];
        func( &set );
    }
    
    byte func( byte (*rule)[10][15] )
    {
        (*rule)[0][0] = 0;  // you need all the (*) stuff just to access 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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. vector of arrays of pointers to structures
    By Marksman in forum C++ Programming
    Replies: 13
    Last Post: 02-01-2008, 04:44 AM
  2. pointers & arrays and realloc!
    By zesty in forum C Programming
    Replies: 14
    Last Post: 01-19-2008, 04:24 PM
  3. Array of Pointers to Arrays
    By Biozero in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 02:31 PM
  4. Pointers and multi dimensional arrays
    By andrea72 in forum C++ Programming
    Replies: 5
    Last Post: 01-23-2007, 04:49 PM
  5. Passing pointers to arrays of char arrays
    By bobthebullet990 in forum C Programming
    Replies: 5
    Last Post: 03-31-2006, 05:31 AM