previous | start | next

15. Pointers and Subscripts

Pointer arithmetic and the dereferencing operator make it possible to use subscripts on pointers just as with arrays.

For example, with the declarations below, p has value &a[0] and

 *p       is the same as a[0]       
 *(p +1)  is the same as a[1]
 *(p + 2) is the same as a[2]
 ...
         

Actually C++ interprets subscripts for ordinary arrays using pointer arithmetic and the dereferencing operator

 a[0] is just *a
 a[1] is just *(a + 1)
 a[2] is just *(a + 2) 
 ...
         

So it is also allowed to use subscripts on pointers and

 p[0] is just *p
 p[1] is just *(p + 1)
 p[2] is just *(p + 2)
 ...        
         


previous | start | next