|
-
July 12th, 2006, 04:16 AM
#1
passing arrays to functions
Hi there.
Its me again!..and this time I have a silly question to put forward. it may sound silly but it has been troubling me from the inside.
I don't really understand why can i pass a one - dimensional (hereafter referred to as 1d) array to a function without ever passing the subscript and for any other dimensional array why do I need to still ignore the 1st subscript but pass the remaining ones.
for ex. if I have a 2d array say deck[4][13] and if I need to print the contents of the array,
then i just pass deck[ ][13] to the function.
Heres the sample function which does the printing:
Code:
void printArray(const int a[][ 13 ])
{
int i=0,j=0;
do
{
if(!j) printf("%4d",j++);//print a right justified 0 in a space of 4
else printf("%5d",j++);//print col. hdrs in a space of 5(rt. justified)
}
while (j<=12);
printf("\n\n");
for ( i = 0; i <= 3; i++ ) {
for ( j = 0; j <= 12; j++ ) //heres the actual printing taking place
printf( "%4d ", a[ i ][ j ] );
printf( "\n\n" );
}
}
How in the world will the compiler allocate memory space if it doesn't exactly know how many elements it needs to have?
Secondly what if I use a pointer to a constant data as the argument in the function header like const int *array instead of const int a[ ][13] and use pointer offset notation to print the elements?
Lastly (this is program specific), in the little code snippet above, I am trying to print the contents of the array. I am trying to have give column numbers above each column in the array. U can run and see it for urself because a pic(a sort of pic) speakes better than a thousand words so to speak. U can use the following code to initialize the array or u can initiaze it to 0 if u like.
Code:
for(row=0;row<=3;row++) //sequential initialization
for(col=0;col<=13;col++) //of every element to current
deck[row][col]=(col+1)+(row*13);//value of i.
What I do not understand is how does the program above works fine even if I remove the else clause! as per my understanding, it should mess up by printing a 0 over column number 0 and column number 1 both and the whole row which contains these column numbers should get displaced to the right by 4 spaces. But it doesn't! I may have overlooked something really obvious here but I can't figure that out. pretty silly u'll say..ya it is..
Hoping to hear from you soon,
Best Regards,
Aijaz Baig.
Last edited by aijazbaig1; July 12th, 2006 at 04:22 AM.
-
July 12th, 2006, 04:37 AM
#2
Re: passing arrays to functions
You do not pass an array to a function, you pass the address of one of its elements. So the compiler allocates the space for the pointer.
It needs the second dimension to do the pointer arithmetic. i.e. from a[0] to a[1] it needs to know by how much to increase the pointer.
-
July 12th, 2006, 04:39 AM
#3
Re: passing arrays to functions
Normally one should pass a pointer to array (array itself is a pointer) to function with dimension as saperate argument. That way you make the function generic.
Code:
void printArray(const int *a[], int xDimension, int yDimension)
{
int i=xDimension,j=yDimension;
do
{
if(!j) printf("%4d",j++);//print a right justified 0 in a space of 4
else printf("%5d",j++);//print col. hdrs in a space of 5(rt. justified)
}
while (j<=12);
printf("\n\n");
for ( i = 0; i <= 3; i++ ) {
for ( j = 0; j <= 12; j++ ) //heres the actual printing taking place
printf( "%4d ", a[ i ][ j ] );
printf( "\n\n" );
}
}
Regards,
Ramkrishna Pawar
-
July 12th, 2006, 09:43 AM
#4
Re: passing arrays to functions
Hello.
What u did krishnaa wouldn't help to do the formatting though it could be used to print the elements of the array.
What seems a plausible explanation to me is that when we pass a 1d array we dont need to pass the size as an array is always passed by reference. So the function has direct access to the array in a way. Hence one can easily check for the NULL character to check for the array end.
However in case of multi dimensional arrays, which are actually stored in contiguous memory locations, we need to know exactly how many elements form a (logical) row so that the correct element could be picked by the compiler. So we need to pass the subsequent subscripts I guess.
please reconfirm or correct me if im wrong here..
Last edited by aijazbaig1; July 12th, 2006 at 09:45 AM.
-
July 12th, 2006, 11:16 AM
#5
Re: passing arrays to functions
I prefer to pass a reference (const if necessary) to my matrix class, and the dimensions can be retrieved from that.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|