We know that we can dynamically create variables using pointers as for example:
int *p = new int(5);
We can access its value using * as
cout << *p;
But this is not the case of the array, consider the below code:
int size_array = 5;
int * p = new int[size_array];
if we need to access the value of the first element, we do the following:
cout << p[0];
but why we can't do the same as for a dynamic variable like above?, i.e. using *:
cout << *p[0];
*porp[0].*(p+1)orp[1]. Et cetera.p[index]is the same as*(p+index), so*pworks just fine whether you usenew intornew int[size_array], just note that in the second case it only accesses the 1st element in the array, unlesspis updated to point at another element. Which you shouldn't do, use a separate pointer for that, if needed.