What is output of this program ?
#include<stdio.h>
main()
{
int a[2][2][2] = { {10,2,3,4}, {5,6,7,8} };
int *p,*q;
p=&a[2][2][2];
*q=***a;
printf("%d----%d",*p,*q);
}
Member • Aug 13, 2012
Member • Aug 13, 2012
Member • Aug 13, 2012
Member • Aug 13, 2012
Member • Aug 13, 2012
silverscorpionWhen you declare the array, it's actually stored as follows:
___________________________
| 10 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
-------------------------------
But, since it's declared as a 3-d array, it should be visualized a little differently. You can think of it as an array containing 2 elements, each of which is an array containing 2 elements each of which is a 2-element array again.
So, if you just specify a, it points to the address of the first element of the array. Specifically, 'a' points to the address of the array
_______
|10 | 2 |
| 3 | 4 |
--------
(Note that a only has 2 elements, one is the 2x2 array shown above. The other is the 2x2 array from the other 4 elements 5,6,7 and 8)
Now, *a refers to the value in the 0[sup]th[/sup] position of the array... so, when you put *a, that returns the same 2x2 array shown above.
Now comes **a. As already said, *(some_array) returns the value of the first element in that array. So, **a returns the first element of the 2x2 array, which is a 1x2 array. And that is
_______
|10 | 2 |
--------
So, when you do it 3 times, ie., when you use ***a, it returns the first value of the 1x2 array, which is 10. Voila!!
Hope it's clear! 😀
Member • Aug 13, 2012
We are not actually using a as a pointer. A pointer stores the address of another variable. Here, we are merely trying to get the value stored in a given address, and that happens to be the address where the array is stored..English-ScaredHats off, #-Link-Snipped-#! Great explanation!
Correct me if I am wrong, but if "a" is declared as array then it can be used as pointer.
The answer to the question #-Link-Snipped-# asked.