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

Rishab said:   4 years ago
@All.

Suppose if you have an array a[10]
And you want to give a[5] = 12.

You can write *(a+i) = 12 (this is nothing but a pointer way of using array)
if you print(a) it will point to the base address of the array.
these are the basics of array.

Now let's work with the question.
if we have 3d array a[10][10][10].
and you want to make a[5][6][7] = 30,
You can use *(*(*(a+5)+6)+7) = 30,

count the stars needed to give the value. 3 stars,
so until 3 stars you will be printing the address
in this case.

"a"
will print address of a
"*a"
will print address of *(a+0) which is same as a
"**a"
will print address of *(*(a+0)+0) which is same as a
but
"***a"
will print VALUE of *(*(*(a+0)+0)+0) which is 1

Hope you all get it.
(11)

Chetan said:   1 decade ago
It depend upon the dimention of the array that how many '*' are needed to get the value.
in case of 1D Array i,e
int arr[]={1,2,3,4};
int *a = arr;
a---- gives the base address and
*a--- gives the value at the base address.

In case of 2d Array i,e
int a[2][3] = {1, 2, 3, 4, 5, 6};
int *a = arr;
a----- gives the base address of the array.
*a---- this also gives the base address and
**a--- return the valuue at the base address

Similarly, in case of 3D Array i,e
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} };

int *a = arr;
a----- gives the base address of the array.
*a---- this also gives the base address
**a--- this also gives the base address and
***a--- return the valuue at the base address

Thus the short cut is No OF '*' when it is EQUAL to the Dimention of the array it returns the value.
For 1D --- 1'*'
2D --- 2'*'
3D --- 3'*'
(1)

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)

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.

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

Preety said:   1 decade ago
Is this problem also have any link with format specifier or its jst with dimension of 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.

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.


Post your comments here:

Your comments will be displayed after verification.