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 4 of 6.
Kishor said:
1 decade ago
Elements stored as:
arr[0][0][0] = 10
arr[0][0][1] = 2
arr[0][1][0] = 3
arr[0][1][1] = 4
arr[1][0][0] = 5
arr[1][0][1] = 6
arr[1][1][0] = 7
arr[1][1][1] = 8
So *p=arr[1][1][1]=8.
And *q=10(first element).
arr[0][0][0] = 10
arr[0][0][1] = 2
arr[0][1][0] = 3
arr[0][1][1] = 4
arr[1][0][0] = 5
arr[1][0][1] = 6
arr[1][1][0] = 7
arr[1][1][1] = 8
So *p=arr[1][1][1]=8.
And *q=10(first element).
(4)
Arvindh said:
1 decade ago
I feel the best way to think of 3D arrays is to imagine a cube and put the numbers in their corresponding vertices.
For 2x2x2 array, we can imagine a cube with a total of 8 vertices, 4 in the front and 4 at the back.
10, 2, 3, 4, 5, 6, 7, 8 can be written here as
10(5) 2(6)
3(7) 4(8)
/*
Where the numbers without brackets are the vertices of the nearer face of the cube and those in brackets are the vertices of the farther face.
*/
Now, a[1][1][1] is the bottom right vertex of the farther face of the cube -- which is 8.
And "arr" is the address of the first element of the array -- here it is 10.
So, we get 10 and 8 as the answers.
For 2x2x2 array, we can imagine a cube with a total of 8 vertices, 4 in the front and 4 at the back.
10, 2, 3, 4, 5, 6, 7, 8 can be written here as
10(5) 2(6)
3(7) 4(8)
/*
Where the numbers without brackets are the vertices of the nearer face of the cube and those in brackets are the vertices of the farther face.
*/
Now, a[1][1][1] is the bottom right vertex of the farther face of the cube -- which is 8.
And "arr" is the address of the first element of the array -- here it is 10.
So, we get 10 and 8 as the answers.
(1)
J G Deepak said:
1 decade ago
1. a[1][1][1] is the last element. So a[1][1][1]=8.
2. The pointer *q always points to the first element of the array arr[][][], because of the initialization of q = (int*) arr. So *q represents the first element of the array.
Therefore the answer is 8,10.
2. The pointer *q always points to the first element of the array arr[][][], because of the initialization of q = (int*) arr. So *q represents the first element of the array.
Therefore the answer is 8,10.
HarshaN said:
1 decade ago
@shubham
No need of casting here as q in an integer point here.So q = (int*) arr; and q = arr; would result in same output.
No need of casting here as q in an integer point here.So q = (int*) arr; and q = arr; would result in same output.
Shubham said:
1 decade ago
Please answer me. What significance for sentence below it.
q = (int*) arr;.
q = (int*) arr;.
Subaa said:
1 decade ago
Nice explained chettan thank you.
Naveen said:
1 decade ago
a[0][1][1] this means 1st block 2nd row 2nd coloumn right. ? its valus is 4. Am I right. ? please explain.
Pooja said:
1 decade ago
Thanks Rahul.
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)
Subhi said:
1 decade ago
Nicely explained by Teklit Ehiopia.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers