C Programming - Pointers - Discussion
Discussion Forum : Pointers - Find Output of Program (Q.No. 11)
11.
What will be the output of the program?
#include<stdio.h>
int main()
{
int arr[2][2][2] = {10, 2, 3, 4, 5, 6, 7, 8};
int *p, *q;
p = &arr[1][1][1];
q = (int*) arr;
printf("%d, %d\n", *p, *q);
return 0;
}
Discussion:
58 comments Page 2 of 6.
Yeswanth said:
1 decade ago
Well said rahul.
Teklit Ehiopia said:
1 decade ago
This is 3D array you can look at this diagram so that easly understand it
arr[1][1][1]==>block 2,row 2,colum 2; ==>[8].
arr mean arr[0][0][0]==>block 1 row 1 column; ==>[10]
1stblock R1| 10 . 2| 2nd block R1| 5 . 6 |
|_______| |_______|
R2 | 3 , 4| R2| 7 . 8 |
|_______| |_______|
C1 C2 C1 C2
arr[1][1][1]==>block 2,row 2,colum 2; ==>[8].
arr mean arr[0][0][0]==>block 1 row 1 column; ==>[10]
(1)
Sagar Kapadiya (Surat) said:
1 decade ago
Value if its a[ 2 ] [ 2 ] [ 1 ].. ?
VENU said:
1 decade ago
@SAGAR
Its 7.
Its 7.
Swanand said:
1 decade ago
Good rahul hope you explain in future like this.
Kiran sp said:
1 decade ago
Hi friends, can any one tell me, how the elements in 3d array are allocated. ?
Tanveerkhan guttal said:
1 decade ago
Nicely explained by Teklit Ehiopia.
Satishp said:
1 decade ago
a[0][0][0]=10;
a[0][1][1] =2;
0 1 ->blocks
0 1 0 1 ->sub blocks
10 2 3 4 5 6 7 8 ->values
0 1 0 1 0 1 0 1 ->sub blocks of sub blocks
3-D REPRESENTATION OF ARRY IS
blocks-subblocks-subblocks of subblocks-values
then a [0] [1] [1] = 4
a [1] [1] [0] = 7 .so i think it is correct.
a[0][1][1] =2;
0 1 ->blocks
0 1 0 1 ->sub blocks
10 2 3 4 5 6 7 8 ->values
0 1 0 1 0 1 0 1 ->sub blocks of sub blocks
3-D REPRESENTATION OF ARRY IS
blocks-subblocks-subblocks of subblocks-values
then a [0] [1] [1] = 4
a [1] [1] [0] = 7 .so i think it is correct.
Subhi said:
1 decade ago
Nicely explained by Teklit Ehiopia.
Chetan said:
1 decade ago
arr[2][2][2], this array representation says that there are 2Blocks each with 2Rows and 2Columns
i,e we can judge that every Block will have 4 Elements each
so Block1-------[10 2 3 4]
i,e {10 2} in Row1
{3 4} in Row2
Again, Block2----[5 6 7 8]
i,e {5 6} in Row1
{7 8} in Row2
Now, let me explain how elments are described in 3D array representaion:- [Block][Row][Column]
so, the member of Block1 are described as below,
10 is [0][0][0]
2 is [0][0][1]
3 is [0][1][0]
4 is [0][1][1]
now, the members of Block2,
5 is [1][0][0]
6 is [1][0][1]
and so on
Thus, arr[1][1][1] is 8
i,e we can judge that every Block will have 4 Elements each
so Block1-------[10 2 3 4]
i,e {10 2} in Row1
{3 4} in Row2
Again, Block2----[5 6 7 8]
i,e {5 6} in Row1
{7 8} in Row2
Now, let me explain how elments are described in 3D array representaion:- [Block][Row][Column]
so, the member of Block1 are described as below,
10 is [0][0][0]
2 is [0][0][1]
3 is [0][1][0]
4 is [0][1][1]
now, the members of Block2,
5 is [1][0][0]
6 is [1][0][1]
and so on
Thus, arr[1][1][1] is 8
(2)
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers