Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

To the first O of the array?

like image 453
user383352 Avatar asked Nov 27 '22 23:11

user383352


2 Answers

Yes.

like image 151
kennytm Avatar answered Dec 16 '22 06:12

kennytm


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]
like image 23
TJ Koblentz Avatar answered Dec 16 '22 05:12

TJ Koblentz