C Programming - Pointers - Discussion
Discussion Forum : Pointers - Find Output of Program (Q.No. 18)
18.
What will be the output of the program assuming that the array begins at location 1002?
#include<stdio.h>
int main()
{
int a[2][3][4] = { {1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 1, 2},
{2, 1, 4, 7, 6, 7, 8, 9, 0, 0, 0, 0} };
printf("%u, %u, %u, %d\n", a, *a, **a, ***a);
return 0;
}
Discussion:
54 comments Page 6 of 6.
Shailesh said:
1 decade ago
No, Chetan is not telling perfectly.
Dear Hari,
a----- gives the base address of the array.
*a---- this also gives the base address and
**a--- return the valuue at the base address
These lines are not 100% correct,written by Chetan.
But the fact is that a represents base adds of first block,
*a=base adds of zeroth row &
**a=base adds of zeroth column.
NOw see this carefully,
***a prints 1,The 1 is not the base adds value,(here base adds word is fake b'z there are 3 base addss representing 1 value) but more clearly it is the vale pointed by the base adds of 1st colm or 0th colm here ,& not by pointed by row's or block's base adds ,i think Hari will get this.
again if you wrote like this *(**a),this will print value which is pointed by base adds of column.
So keep in mind clearly that,
a ---- base adds of array represented by block & not by any other.
*a --- base adds of array represented by row & not by any means.
**a ---base adds of array represented by colm & not by any other
*(*a+1)---prints value of 1st row,because *a =base adds of 0th row,but however **a+1 will print value of 1st column, i.e. value 2 in above example.
Dear Hari,
a----- gives the base address of the array.
*a---- this also gives the base address and
**a--- return the valuue at the base address
These lines are not 100% correct,written by Chetan.
But the fact is that a represents base adds of first block,
*a=base adds of zeroth row &
**a=base adds of zeroth column.
NOw see this carefully,
***a prints 1,The 1 is not the base adds value,(here base adds word is fake b'z there are 3 base addss representing 1 value) but more clearly it is the vale pointed by the base adds of 1st colm or 0th colm here ,& not by pointed by row's or block's base adds ,i think Hari will get this.
again if you wrote like this *(**a),this will print value which is pointed by base adds of column.
So keep in mind clearly that,
a ---- base adds of array represented by block & not by any other.
*a --- base adds of array represented by row & not by any means.
**a ---base adds of array represented by colm & not by any other
*(*a+1)---prints value of 1st row,because *a =base adds of 0th row,but however **a+1 will print value of 1st column, i.e. value 2 in above example.
Karthik said:
1 decade ago
@Shailesh thank you very much.
Karthik said:
1 decade ago
@Chetan thank you very much.
Vishwas said:
1 decade ago
a is a 3d array;
consider a[2][3][4]
subscript 2 represents 2 blocks
subscript 3 represents 3 rows
subscript 4 represents 4 columns
i.e. a is a 3-d array with 2 blocks where each block is a 3*4 matrix
therefore arrangement of elements in a can be visualized as:
a={
block1:
[1 2 3 4
5 6 7 8
9 1 1 2],
block 2:
[2 1 4 7
6 7 8 9
0 0 0 0]
}
1)Now a means/equivalent to &a[0][0][0]
which points to the first block's address which is 1002
2)*a points to first block,first row,which again has address 1002
3)**a points to first block,first row,first col element, address=1002
4)***a is the value stored in first block,first row,first col which is 1
hence we get 1002,1002,1002,1
consider a[2][3][4]
subscript 2 represents 2 blocks
subscript 3 represents 3 rows
subscript 4 represents 4 columns
i.e. a is a 3-d array with 2 blocks where each block is a 3*4 matrix
therefore arrangement of elements in a can be visualized as:
a={
block1:
[1 2 3 4
5 6 7 8
9 1 1 2],
block 2:
[2 1 4 7
6 7 8 9
0 0 0 0]
}
1)Now a means/equivalent to &a[0][0][0]
which points to the first block's address which is 1002
2)*a points to first block,first row,which again has address 1002
3)**a points to first block,first row,first col element, address=1002
4)***a is the value stored in first block,first row,first col which is 1
hence we get 1002,1002,1002,1
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers