Replies
Welcome, guest
Join CrazyEngineers to reply, ask questions, and participate in conversations.
CrazyEngineers powered by Jatra Community Platform
-
@prototype-G9Gn5k • Aug 13, 2012
p=&a[2][2][2];
Only if you're trying to take the address of the last element in the array. If you actually want the start address of the array, then it should be only &a.
*q=***a;
How come the "a" became pointer suddenly?
So basically, the output of the program is error because "a" is not a pointer. Not a compilation error but a logical error. -
@hare-singh-nayak-WEmcz0 • Aug 13, 2012
actually sir there is no error
when i am running this code the output is like that
5641496----10
and i am not able to understand what is happending here so please explain in lit bit detail.. -
@silverscorpion-iJKtdQ • Aug 13, 2012
When 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! 😀 -
@saandeep-sreerambatla-hWHU1M • Aug 13, 2012
Hats 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. -
@hare-singh-nayak-WEmcz0 • 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! 😀
Tank you silverscorpion very nice explanation.. -
@silverscorpion-iJKtdQ • 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.
The * operator gets the value stored in whatever address we use it with. And here, we use it with a. And when a is declared as an array, the name 'a' alone, without any index, 'points' to the first element of the array. So, saying just a and saying &a[0] are same.
We can declare another variable, say b, as an int. And then get the value stored in that variable by using *(&b). In this array, we're doing the same, just that, instead of *(&a[0]), we use *a