Why we can simply write like:- ptr =a; | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Why we can simply write like:- ptr =a;

Instead of ptr =&a[0];

12th May 2019, 3:20 AM
Aशीsh
Aशीsh - avatar
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;
12th May 2019, 3:25 AM
Hatsy Rei
Hatsy Rei - avatar
+ 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!
12th May 2019, 3:26 AM
🇮🇳Omkar🕉
🇮🇳Omkar🕉 - avatar