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.
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 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)
Cool said:
1 decade ago
Good one dude! (rahul).
(1)
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)
Chandan said:
7 years ago
Good explanation @Rahul.
Emanuel said:
9 years ago
I think it should be q=arr;.
Nikhil Gavali said:
8 years ago
It will not read the matrix only if it [2][1][1] because it is 3*3 matrix and in above program we consider matrix as 2*2.
Swap said:
8 years ago
Thanks all.
Prathyusha said:
8 years ago
Thank you @Chethan.
Prathyusha said:
8 years ago
Can anyone explain how 4D array can be represented with an example?
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers