Pointers

Contents

Pointers#

A pointer is a typed address. T *p reads “p is a pointer to T”; *p reads the value through the pointer.

int  x = 10;
int *p = &x;
*p = 20;                     /* x is now 20 */

const placement matters: where it sits determines what is immutable.

const char *s = "hello";     /* pointer to immutable chars */
char *const HEAD = buf;      /* const pointer to mutable chars */

Pointer arithmetic moves by sizeof *p.

int xs[3] = {10, 20, 30};
int *p = xs;
p++;                         /* now points to xs[1] */

NULL is the null pointer; C23 adds the nullptr keyword that behaves more like nullptr in C++.

References#

  • Arrays for the array-decays-to-pointer rule.

  • Structs for the -> form on struct pointers.