0

Regarding the convention of arrays allocated on the heap versus non array

int *p1 = new int; *p1=5; // p1 will now hold an address that is pointing to a memory location on the heap and *p1 // will refer to the actual data at that address // however int *p2 = NULL; p2 = new int[20]; p2[0]=2; delete []p2; /* Now p2 holds the memory address of the memory location where the array will be stored, and p2[index] refers to the actual data inside the memory location pointed by p2, corresponding to index, why isn't the same convention used for arrays as for non arrays ? Wasn't *p2[0] supposed to refer to the actual data pointed by pointer p2 by the same convention above ? */

5th Jun 2017, 11:00 AM
Feraru Silviu Marian
Feraru Silviu Marian - avatar
1 Réponse
0
it is the same. you canuse *(p+index) or p[index]. p is like an array of length 1.
24th Jun 2017, 9:58 PM
Volker Milbrandt
Volker Milbrandt - avatar