Skip to content
Tutorials Center
My learning

Topics

C Reference ยท Operators and Expressions

Address-of (&) and dereference (*) in C

Reference for the C unary operators & and *: taking an object's address, reading and writing through a pointer, null pointers and undefined behavior.

The unary & operator yields the address of an object, as a pointer to that object's type. The unary * operator does the opposite: applied to a pointer, it designates the object the pointer points to, so you can read it or assign to it. The two are inverses: *&x is x, and &*p is p (with no access made).

Pointers are how C passes objects by reference, walks arrays and builds linked structures, so these two operators appear in almost every non-trivial program.

C
int x = 10;
int *p = &x;     /* p holds the address of x */
*p = 20;         /* x is now 20 */

Try it

C Reading and writing through a pointer

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

C Arrays, structs and the null pointer

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Syntax

C
& cast-expression      /* address-of */
* cast-expression      /* indirection (dereference) */
Operator Operand Result
& An lvalue (an object or function), a * expression or a [] expression. A pointer to the operand, of type T * when the operand has type T.
* A pointer. An lvalue designating the pointed-to object (or function).

Errors / undefined behavior:

  • Dereferencing a null pointer, a dangling pointer (to an object whose lifetime ended), an uninitialized pointer, or a pointer past the end of an array is undefined behavior.
  • & cannot be applied to a bit-field, a register object, or a non-lvalue such as &(a + b) or &5: these are compile errors.
  • Dereferencing a void * is a compile error: cast it first.

Usage notes

  • Pass by reference. C passes arguments by value, so a function receives a copy of a pointer; writing through it changes the caller's object. See pointer parameters.
  • Declarations versus expressions. In int *p, the * is part of the declarator ("p is a pointer to int"). In *p = 3 it is the dereference operator. The same symbol, different roles. The & in a declaration never exists in C (references are C++).
  • Precedence. Both are unary, bind tighter than arithmetic and are right to left associative. *p++ means *(p++), while (*p)++ increments the pointed-to value. *p.x is *(p.x), not (*p).x; use p->x.
  • & and * on arrays and functions. &arr has type int (*)[N], a pointer to the whole array, while arr decays to int *. For functions, &f, f and *f all denote the same function, which is why (*fp)(x) and fp(x) are both legal.
  • Null is checked, not detected. C does not stop you from dereferencing NULL; the usual result is a crash, but nothing is guaranteed. Test pointers that may be null before use.
  • Lifetime. Never return the address of a local variable: the object is gone when the function returns. See storage duration.
  • const is preserved. &c for a const int c has type const int *; converting it to int * discards the qualifier and writing through it is undefined behavior.
  • Printing addresses with %p needs a (void *) cast, and the values differ from run to run, so do not rely on them.