C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 11-10-2009, 04:45 PM   #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.
hugo2x is offline   Reply With Quote
Old 11-10-2009, 05:02 PM   #2
Registered User
 
Join Date: Apr 2006
Posts: 1,193
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++.
__________________
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.

Last edited by King Mir; 11-10-2009 at 05:24 PM.
King Mir is offline   Reply With Quote
Old 11-10-2009, 05:35 PM   #3
Registered User
 
Join Date: Sep 2006
Posts: 2,502
Could you just pass both dimensions? There had to be a variable for the rows when you put the data into the array.
Adak is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Screwy Linker Error - VC2005 Tonto C++ Programming 5 06-19-2007 02:39 PM
Calling a Thread with a Function Pointer. ScrollMaster Windows Programming 6 06-10-2006 08:56 AM
<Gulp> kryptkat Windows Programming 7 01-14-2006 01:03 PM
airport Log program using 3D linked List : problem reading from file gemini_shooter C Programming 3 03-04-2005 02:46 PM
Hi, could someone help me with arrays? goodn C Programming 20 10-18-2001 09:48 AM


All times are GMT -6. The time now is 07:49 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22