Pointers and arrays
- Lvalues and Rvalues
&— address-of operator*— indirection (or dereference) operator- If
Pis a pointer to an integer, thenP+iis a pointer to thei’th integer after the integer referenced byP. P[i]is*(P+i).- If
Pis an array of integers, thenPis a constant pointer to an integer. P++incrementspto point to the next integer in memory.NULLin C is similar tonullin Java.- Pascal had pointers:
@Pfor&PandP^forP*.
It only gets worse
Suppose both P and Q are pointers
to integers.
(int) &P[5]is(int) P + 5 * sizeof(int)(int) &P[5] - (int) &P[3]is2 * sizeof(int)P+iandP-iandP<QandP>QandP++andP--are only defined if the relevant pointers are elements of the same arrayP + Qis not allowed for pointersvoid *is the type of a generic pointer(long) (void *) &P[5]is weird but implies intent
Trying it out
Take a look at Problem 16 from the Fall 2014 Final exam.