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 3 of 6.

Pooja said:   1 decade ago
Thanks Rahul.

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.

Subaa said:   1 decade ago
Nice explained chettan thank you.

Shubham said:   1 decade ago
Please answer me. What significance for sentence below it.

q = (int*) arr;.

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.

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.

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

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

Ibrahem said:   1 decade ago
Try this in this code :

printf("%d\n",*arr);
........

The output is garbage ! why ?
p= arr;
So *p = *arr ?

Punit said:   1 decade ago
Why the statement q = (int*) arr? Why this type casting is done?
(1)


Post your comments here:

Your comments will be displayed after verification.