[prev] [index] [next]

Aside: Pointer Arithmetic (cont)

For a pointer declared as   T *p;   (where T is a type)
  • if the pointer initially contains address A
    • executing   p = p + k;   (where k is a constant)
      • changes the value in  p  to   A + k*sizeof(T)
The value of k can be positive or negative.

Example:

int a[6];   (addr 0x1000)       char s[10];   (addr 0x2000)
int *p;     (p == ?)           char *q;      (q == ?)
p = &a[0];  (p == 0x1000)       q = &s[0];    (q == 0x2000)
p = p + 2;  (p == 0x1008)       q++;          (q == 0x2001)