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 1 of 6.
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)
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)
RAHUL SHARMA said:
1 decade ago
Here the 3-D array the value can be store as:
and the *arr represent the first number of a array is 10.
Then hence the number is (8,10).
a[2][2][2]={
{
10,2
3,4 // First Block
}
{
5,6
7,8 //Second Block
}
}
a[ 0 ] [ 0 ] [ 0 ] =10;
| | |
Block Rows Columns
a [ 1 ] [ 1 ] [ 1 ] = 8;
| | |
2'Block 2'Row 2'column
and the *arr represent the first number of a array is 10.
Then hence the number is (8,10).
(38)
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)
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.
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.
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)
Abhijit said:
1 decade ago
@haneef:
a[2][1][1] cannot be accessed, coz array is initialized only for the 2nd dimension(i.e 0,1) a[2]... will be like accessing the thrid element which is not initialized.
Hope you got it.
a[2][1][1] cannot be accessed, coz array is initialized only for the 2nd dimension(i.e 0,1) a[2]... will be like accessing the thrid element which is not initialized.
Hope you got it.
(1)
Sagar said:
1 decade ago
It is three dimensional array,
so,arr[1][1][1]=8,
*p,
p=&arr[1][1][1],
*p means value at address i.e=8.
(int*)arr always consist first value as,
starting value at arr address,so
o/p= 8,10
so,arr[1][1][1]=8,
*p,
p=&arr[1][1][1],
*p means value at address i.e=8.
(int*)arr always consist first value as,
starting value at arr address,so
o/p= 8,10
(1)
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.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers