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

Kavya said:   8 years ago
Well said @Kishore.

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

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)

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.

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

Sagar said:   9 years ago
(*(*(*a+i)+j)+k)
*a=a+0
**a=(*(*a+0)+0)
***a=*(*(*(a+0)+0)+0)

So we are not moving the pointer anywhere.

Sowmya said:   9 years ago
*a actually returns the value but how come it returns address? Can anyone explain?

Student said:   9 years ago
@Chetan.

Good job, Thank you.

Praveena said:   9 years ago
Thanks, @Alisha. Nice explanation.

Hareesh said:   10 years ago
Can't understand please explain?

Mahendra said:   1 decade ago
a[i][j][k] is nothing but *(*(*(a+i)+j)+k).

Where 'a' is the base address of the 3D array.


Post your comments here:

Your comments will be displayed after verification.