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;
}
8, 10
10, 2
8, 1
Garbage values
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
58 comments Page 1 of 6.

RAHUL SHARMA said:   1 decade ago
Here the 3-D array the value can be store as:

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)

Nancy said:   1 decade ago
Good explanation rahul.

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
(1)

Haneef said:   1 decade ago
Rahul. What the value if its a[ 2 ] [ 1 ] [ 1 ]. ?

Cool said:   1 decade ago
Good one dude! (rahul).
(1)

Sanjay said:   1 decade ago
Thank you sagar.

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.
(1)

Harishgoud said:   1 decade ago
Exactly the answer is right Mr. Rahul.

Arvind said:   1 decade ago
Nice explaind rahul.

Mani said:   1 decade ago
Thanks for the clear explanation raghul


Post your comments here:

Your comments will be displayed after verification.