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

Emanuel said:   9 years ago
I think 1002,1 and after it has no sense.

Ankita said:   8 years ago
a, *a, **a have format specifiers as "%u" which tells the compiler to print the address at a[0]. So, it prints 1002 thrice, each for a, *a, **a.

However, for ***a, the format specifier is "%d" which tells the compiler to print the value at that particular address. So it prints 1 for ***a.

Sangaraj Desai said:   8 years ago
Since the name of the array holds base address of the array hence a=1002
here 1002 something like a value if we use *a then it will return whatever stored in a since a holds 1002
so *a=1002
similarly **a=1002.

For ***a they have mentioned the %d format specifier hence it will return whatever value stored in a[0][0][0]=1..if they specified %u instead of %d once again output will be 1002
hence,
a=1002
*a=1002
**a=1002
***a=1(because of format specifier %d in printf)
(1)

Kavya said:   8 years ago
Well said @Kishore.

%u for address.
%d for value.
(1)

Shilpa said:   8 years ago
Well done @Sangaraj.

Ashu said:   8 years ago
Why ***a=1? Please explain it.

Payal said:   8 years ago
Why ***a=1?

Please explain it.

Rohan said:   7 years ago
@Ashu, @Payal.

printf("%u, %u, %u, %d\n", a, *a, **a, ***a);
/* It specifies %d that's why the data stored in ***a is printed instead of its address.... */.

Muhammad Fouad said:   7 years ago
Hey guys, just want to figure out some concepts:-

The array name (without a subscript) is a pointer to the first element in the array.

So, when saying printf( "%d, %d, %d and %d", a, *a, **a, ***a );

The first a is a pointer to the first element, i.e returns the address of the array or the first element of the array, the second a is a pointer to a pointer, also returns am address, the third one also the same, but the last one is exceptional, because the compiler already knows that a 3D array was declared, so a maximum of triple dereferencing statements is obtained, because the forth a which is ***a will return the actual value which is 1!!.

Hope you got it.

Pattu said:   7 years ago
Thanks for the answer @Shriram.


Post your comments here:

Your comments will be displayed after verification.