Thread: Is it possible to determine amount of rows of a 2-d array passed in a function

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    13

    Is it possible to determine amount of rows of a 2-d array passed in a function

    I am trying to make a function that accepts a an array by reference and determines the amount of rows in the 2-d array.
    Is there a way to do this? (I do know the columns)
    Code:
    void printer(int [][3]);
    int main(void)
    {
    	int array1[][3]={{ 5,9, 0},
    			{14,7,13},
    	                 { 2,10,8}};
    	int array2[][3]={{ 5,9, 0},
    	                 {14,7,13},
                              {55,10,9}
    	                  { 2,10,8}};
    	
    	printer(array1);
            printer(array2);
    }
    
    void printer(int ary[][3])
    {
    	int rows = ////// some way to calculate 3 from first instance and 4 for second instance, I have tried sizeof(array)/sizeof(int) but it only calculates the size of the address of array(0Xff244da) */
    	
    }
    Any help would greatly be appreciated!
    Last edited by hugo2x; 11-10-2009 at 04:55 PM.

  2. #2
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    If by rows you mean the first dimension of the array, that is omitted from the deceleration, then there is no way given the prototype. You can do this:
    Code:
    void printer(int (*ary)[4][3])//pointer to 4x3 array
    {
            int cols = sizeof(**ary)/sizeof(***ary);
            /*size of each row devided by the size of each element in each row. (sizeof(***ary)==sizeof(int))
            cols should equal 3.*/
    	int rows = sizeof(*ary)/sizeof(**ary); 
            /*This computes the size of the whole array divided by the size of each row.
            rows should equal 4.*/
    	(*ary)[1][1]=5;/*this is how to access the array. */
            ary[0][1][1]=5;/*this is also valid */
    }
    It's not as ugly in C++.
    Last edited by King Mir; 11-10-2009 at 05:24 PM.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Could you just pass both dimensions? There had to be a variable for the rows when you put the data into the array.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  2. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 08:56 AM
  3. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM