Skip to content
Tutorials Center
My learning

Topics

C Reference ยท Types and Literals

Pointers in C

Reference for C pointers: declaring with *, taking addresses with &, dereferencing, NULL and nullptr, pointers to pointers, const placement and common bugs.

A pointer is a variable that holds the address of another object (or function). The type of the pointer records what it points to: an int * points at an int. You get an address with unary & and reach the object again with unary *, called dereferencing.

Pointers make it possible to modify a caller's variable, walk through arrays, build linked structures and allocate memory at run time. They also make it easy to crash: a pointer that is null, uninitialized, or dangling must not be dereferenced.

C
int x = 7;
int *p = &x;     /* p holds the address of x */
*p = 8;          /* x is now 8 */

Try it

C Address-of and dereference

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 const placement and null checks

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
type *name;              /* pointer to type */
type *name = &object;
*name                    /* dereference: the object pointed to */
&object                  /* address of */
name->member             /* same as (*name).member */
Part Description
type * Pointer to type. void * points to an unknown type and converts to and from any object pointer type without a cast.
& Gives the address of an lvalue; the result has type "pointer to that type".
* Dereferences a pointer, yielding an lvalue.
NULL, nullptr Null pointer constants. nullptr (C23) has type nullptr_t from <stddef.h>.

Returns: *p is the object at the address in p.

Errors / undefined behavior: dereferencing a null, uninitialized, dangling (freed or out-of-scope) or misaligned pointer is undefined behavior. Reading through a pointer of an incompatible type violates strict aliasing.

Usage notes

  • Initialize every pointer, to a valid address or to nullptr/NULL. Declare it where you can assign it.
  • int *a, b; declares one pointer and one int. The star binds to the name, not the type.
  • Reading const: read declarations right to left. const int *p is a pointer to a constant int; int *const p is a constant pointer to an int.
  • Pointers are passed by value. A function receives a copy of the address, so reassigning the parameter does not change the caller's pointer; use a pointer to pointer for that.
  • Never return the address of a local variable. The object is gone when the function returns.
  • Pointer comparison: == and != work on any two pointers of compatible type; < and > are defined only within the same array.
  • Do not print addresses in tests; values differ between runs. %p expects a void * argument.
  • Function pointers and object pointers are distinct; converting between them is not portable standard C.