dimanche 28 janvier 2018

Why does the [] notation work with pointers to arrays on the free store? [duplicate]

This question already has an answer here:

int* pd = new int [5];
pd[1] = 10; // now the second element of the array equals 10.

I'm wondering why this notation is allowed, since I've never seen the [] in conjunction with pointers before.

Is it because normal array variables point to their first element, and pointers to arrays do this as well? Which leads me to believe pointers to arrays and arrays themselves are the same thing.

int arr [5];
int* pd = arr; // this statement seems to show how the pointer and the array are identical.

Aucun commentaire:

Enregistrer un commentaire