Thread: Multidimensional Arrays

  1. #1
    Registered User
    Join Date
    Apr 2006
    Larnaca in Cyprus

    Multidimensional Arrays

    Why is not essential to give the size of the first dimension ---eg. int func (int array [] [20], int x));--- when declaring a two dimensional array as a parameter of a function?

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    It just needs to know the length of each row. It doesn't care how many rows you have, that's your job to keep track of. It's the same reason you aren't required to pass the length of a single array:
    void foo( int bar[] );
    It's actually because it changes arrays to a pointer to their first element's type when it passes them to a function.

    Hope is the first step on the road to disappointment.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    The alternative way of saying it (which also reinforces the "it's a pointer to" idea) is
    int func (int (*array)[20], int x);

    There is no size (empty or otherwise), it's all the same to the compiler.

    You can do this if you want.
    // even if you specify a size, it doesn't make any difference.
    int func (int array [5][20], int x);
    int main ( ) {
      int a[100][20], b[200][20], c[20];
      func( a, 100 );
      func( b, 200 );
      func( &c, 1 );
    Here's a simpler example
    size_t strlen( char s[100] ); // not char* s
    does not limit you to working out the length of strings which are only 100 characters long.
    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 in a function prototype
    By Enanito01478 in forum C Programming
    Replies: 2
    Last Post: 10-11-2009, 01:10 AM
  2. Multidimensional arrays
    By Niels_M in forum C Programming
    Replies: 51
    Last Post: 09-12-2009, 03:16 PM
  3. Multidimensional Arrays
    By jordanguyoflove in forum C Programming
    Replies: 4
    Last Post: 10-16-2008, 06:16 PM
  4. Pointers to Multidimensional Arrays
    By kidburla in forum C Programming
    Replies: 10
    Last Post: 10-29-2005, 10:45 PM
  5. Multidimensional arrays in Korn shell
    By Yasir_Malik in forum Tech Board
    Replies: 3
    Last Post: 04-11-2004, 02:16 PM