Consider this code fragment :
printf("%d %d %d",a,&a,*a);
This code is giving unexpected result!
The value of “*a” is 1, which seems to be correct.
But how come compiler is giving similar values of “a” and “&a”??
And even if “a” and “&a” have similar values, then in that case, should’t “*a” be the same as “a” and “&a” ?? Since “a” is pointing to itself.
In printf , since you have used %d , a and &a would give garbage values. Check here. In your case, they are just giving same garbage values as they are pointing to the same address location. To know more about pointers in C/C++ , read this and this.
Arrays are basically pointers. The name of the array is the first memory location from where the array begins.
In printf("%d %d %d",a,&a,*a);
a represents the address of the location from which the array starts. &a is the usual way of saying the address of a, which is again the start location of the array. This is why they are giving similar answer. The last one, *a is the value at pointer a, i.e. the value at the location from which the array starts, which is the first element of the array.
Thanks for the answer. But I feel that’s not the reason. %d just gives decimal system equivalents of the address, not garbage values.
Your answer seems good.
But in this case &a means “address of a”. a is itself an address. So it just implies “address of an address” which is quite absurd. Isn’t it?
I mean,since the decimal representation of starting address of a is out of int range , it is giving garbage value. And if we use %lld in printf , we will get correct decimal representation of address. See here. eIemnT - Online C Compiler & Debugging Tool - Ideone.com
ok now i got your point.
but %d works with my compiler as the address does’t exceed the int range.