C Programming - Pointers - Discussion

Discussion Forum : Pointers - Find Output of Program (Q.No. 18)
18.
What will be the output of the program assuming that the array begins at location 1002?
#include<stdio.h>

int main()
{
    int a[2][3][4] = { {1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 1, 2}, 
                       {2, 1, 4, 7, 6, 7, 8, 9, 0, 0, 0, 0} };
    printf("%u, %u, %u, %d\n", a, *a, **a, ***a);
    return 0;
}
1002, 2004, 4008, 2
2004, 4008, 8016, 1
1002, 1002, 1002, 1
Error
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
54 comments Page 3 of 6.

Vasavi said:   1 decade ago
*a means value, right. So *a=1 have to print. But why here *a=1002 is printed? any one explain me clear.

Vignesh said:   1 decade ago
Starting address of each array is 1002 in printf the format specifier is a %U thats why it print address of that array.

Naveena said:   1 decade ago
I just tried executing this pgm for gnu compiler, it does not depend on the format specifier.

printf("%u, %u, %d, %d\n", a, *a, **a, ***a);

Also gives the same output.

Preety said:   1 decade ago
Is this problem also have any link with format specifier or its jst with dimension of array?

Preety said:   1 decade ago
When we use %u and then use either *p or &p. What will they print the value or address?

Kishor said:   1 decade ago
We have to pay attention towards specific format used.

To get address printed we use "%u" and for value at address "%d" is used.

Thats how given n programme.

Nilesh said:   1 decade ago
Thanks vishwas.

Vishwas said:   1 decade ago
a is a 3d array;
consider a[2][3][4]
subscript 2 represents 2 blocks
subscript 3 represents 3 rows
subscript 4 represents 4 columns
i.e. a is a 3-d array with 2 blocks where each block is a 3*4 matrix
therefore arrangement of elements in a can be visualized as:
a={
block1:
[1 2 3 4
5 6 7 8
9 1 1 2],
block 2:
[2 1 4 7
6 7 8 9
0 0 0 0]
}
1)Now a means/equivalent to &a[0][0][0]
which points to the first block's address which is 1002
2)*a points to first block,first row,which again has address 1002
3)**a points to first block,first row,first col element, address=1002
4)***a is the value stored in first block,first row,first col which is 1
hence we get 1002,1002,1002,1

Karthik said:   1 decade ago
@Shailesh thank you very much.

Karthik said:   1 decade ago
@Chetan thank you very much.


Post your comments here:

Your comments will be displayed after verification.