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.

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.

Emanuel said:   9 years ago
I think it should be q=arr;.

Azagumozhi.M said:   9 years ago
@Teklit. Clear explanation.

Rashmu Jho said:   9 years ago
Nice explanation @Rahul.

Navino said:   9 years ago
@Rahul Sharma.

Very good explanation, Thank you.

Yashwanth said:   10 years ago
Clearly explained. Thank you guys.

Priyanka said:   10 years ago
@Megha.

There is only two block present i.e. 0 & 1. So value not present at this location.

Megha said:   10 years ago
What the value if it is a[ 2 ] [ 1 ] [ 1 ]? Please someone explain.

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

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

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

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


Post your comments here:

Your comments will be displayed after verification.