Search code examples
c++arraysmemory-managementdynamic-memory-allocationdynamic-arrays

O* p = new O[5]; What does p point to?


To the first O of the array?


Solution

  • Exactly. *p and p[0] are the same. Here are some neat features you want to know:

    • "Pointer notation" generally refers to using the 'dereference' (or 'indirection') operator
    • "Array notation" generally refers to using the brackets and offset value

    You can represent an address in memory using either interchangeably:

    • *p is equivalent to p[0]
    • *(p+1) is equivalent to p[1], and more awesomely also equivalent to 1[p]

    NOTE:

    • As noted in another response, the general form is that *(p+i) is equivalent to p[i]
    • Also, please don't use i[p]