[prev] 71 [next]

Pointer arithmetic

Pointers can "move" from object to object by pointer arithmetic

For any pointer T *p;,   p++ increases p by sizeof(T )

Examples (assuming 16-bit pointers):

char   *p = 0x6060;  p++;  assert(p == 0x6061)
int    *q = 0x6060;  q++;  assert(q == 0x6064)
double *r = 0x6060;  r++;  assert(r == 0x6068)

A common (efficient) paradigm for scanning a string

char *s = "a string";
char *c;
// print a string, char-by-char
for (c = s; *c != '\0'; c++) {
   printf("%c", *c);
}