To the first O of the array?
+7
A:
p contains the address of the first O of the array.
Indexing happens like so:
p[i] = *(p+i); //note the pointer arithmetic
Paul Nathan
2010-07-15 16:18:10
+10
A:
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 top[0]
*(p+1)
is equivalent top[1]
, and more awesomely also equivalent to1[p]
NOTE:
- As noted in another response, the general form is that
*(p+i)
is equivalent top[i]
- Also, please don't use
i[p]
tjko
2010-07-15 16:24:49
I don't think the syntax that allows '1[p]' is awsome. I think the language is confusing enough for beginners that we don't need to point out the dark corners of the language (they will be found as the person gets better).
Martin York
2010-07-15 18:29:37
I agree with you about the language being confusing, but the reason I consider this particular point "awesome" is that it implicitly gives us intriguing information about the language's specifications. I think understanding that array notation is simply pointer arithmetic is overlooked. If one knows about pointer arithmetic (commutative property holds) and realizes that the syntax allows for `i[p]`. Then one can gather that these two "notations" are in some ways the same to the compiler -- that's awesome!
tjko
2010-07-15 20:29:12
+1 for a more than one sentence answer. -1.9 for mentioning the `1[p]` abomination. net 0 (I rounded up).
caspin
2010-07-15 22:57:47