+ 2
Why we can simply write like:- ptr =a;
Instead of ptr =&a[0];
2 Answers
+ 12
Array identifiers, when used, return the address of the first element in the array. The notation
a[0]
actually translates to
*(a + 0)
Likewise, a[1] is *(a+1), a[2] is *(a+2) and so on. Assigning a to a pointer is hence legitimate, since a represents an address which can be stored into a pointer of the corresponding type.
Try doing
int a[5];
std::cout << a;
+ 12
This is because array name is always pointing towards first element of array
So address of first element in array i.e &a[0] is same as a because array name itself certainly gives address of first element stored in it!