Thread: passing an array to a function (2d array)

  1. #1
    Registered User
    Join Date
    Dec 2002
    Posts
    221

    passing an array to a function (2d array)

    how
    is it like


    void function (matrix[][])





    and calling it

    function (matrix[10][10]);
    ??

  2. #2
    Registered User
    Join Date
    Jan 2003
    Posts
    115
    try it and see

    but yes its along the lines of

    type arrayname( type [][] )

    standard 2d-array function prototype.
    there are only 10 people in the world, those who know binary and those who dont

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    More like
    Code:
    #define ROWS 10
    #define COLS 10
    
    void foo(char a[][COLS])
    {
    }
    
    int main(void)
    {
      char myarray[ROWS][COLS];
      foo(myarray);  
      return 0;
    }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    Registered User
    Join Date
    Dec 2002
    Posts
    221
    why do u only add the number inside the last brackets only?

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by revelation437
    why do u only add the number inside the last brackets only?
    Because that's the way C works. If you specify one dimension, you have to specify every dimension to the right of it. You always have to specify the last one.

    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    The compiler needs to know the array size in order to do pointer arithmatic. In this snippet:
    >>void foochar(char name[]);
    the compiler knows that the size of each element in the array is sizeof(char). So when you do name[1], you are adding sizeof(char) to name.

    In this example:
    >>void foochar(char name[][10]);
    the compiler knows the size of name[0][0] is sizeof(char) (as above) and name[0] is size 10*sizeof(char). If you didn't specify the array size in the second brackets, the size equation becomes x*sizeof(char) and your compiler will complain (mine says something like "Size of the type 'signed char[]' is unknown or zero").

    Code:
    #include <stdio.h>
    
    int main(int argc, char *argv[])
    {
      char a[10][10][10];
      printf ("sizeof(a[0][0][0]) = %d\n"
              "sizeof(a[0][0])    = %d\n"
              "sizeof(a[0])       = %d\n",
              sizeof(a[0][0][0]),
              sizeof(a[0][0]),
              sizeof(a[0])
              );
      return(0);
    }
    
    /*
    
    Output:
    
    sizeof(a[0][0][0]) = 1
    sizeof(a[0][0])    = 10
    sizeof(a[0])       = 100
     
     */
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. passing 2d array to function
    By Tupcia in forum C Programming
    Replies: 3
    Last Post: 04-13-2008, 11:33 AM
  2. Replies: 1
    Last Post: 10-21-2007, 07:44 AM
  3. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  4. Passing a 2d Array of pointers to a Function
    By miclus in forum C Programming
    Replies: 6
    Last Post: 09-11-2004, 07:34 AM
  5. passing 2d array in to function
    By Kings in forum C++ Programming
    Replies: 1
    Last Post: 01-16-2003, 11:35 AM