mardi 10 mars 2015

What is the difference between the C++ array operator and *(array + index) if any?

I frequently use code like the following to index items out of arrays just to remind myself that the array value is a pointer to the first element of the array.



int array[] = {0,1,2,3,4,5};
*(array + 2) = 42;


While it looks a little ugly, from time to time I actually prefer it to the traditional [] operator.



int array[] = {0,1,2,3,4,5};
array[2] = 42;


Other than making other people who may read my code in the future a little angry, are there any consequences to using pointer arithmetic to index an array over the [] operator?


Aucun commentaire:

Enregistrer un commentaire