YOUR C LEARNING COMPANION

Find your next idea.

Learning path
Memory & dataChapter 23 of 73

Understanding pointers

Build a clear model of pointer objects, their targets, aliasing, null values, and indirect access through function parameters.

9 min readFoundationsC17 baseline
Offline reading
In this chapter10 sections

A pointer lets code refer to an object indirectly. That is useful when a function needs access to a caller’s value or a program needs to work with a buffer. Start with the pointer and its target as two separate objects.

Need the ideas side by side? Open the concept guide.

1. A pointer object and its target are separate

In int *p, p has pointer-to-int type. The declaration’s * helps describe the type; it does not read another object. In the expression *p, unary * is indirection: with a suitable valid pointer, the expression designates the pointed-to object. Context gives the symbol its role.

Pointer object p&count
Target object count4
p and count are separate objects. The arrow shows a relationship, not a fixed numeric address or a guaranteed physical layout.
C17 · illustrative fragment
int count = 4;
int *p = &count;
*p = 9;

The initializer &count supplies a pointer to count. Assigning through *p changes count; it does not change the address stored in p. The pointer object has its own type, storage, and lifetime. Its target has its own type, storage, and lifetime too.

2. Copying a pointer copies the relationship

C17 · illustrative fragment
int left = 4;
int right = 8;
int *p = &left;
int *q = p;
p = &right;
*q = 6;

After q = p, both pointer values refer to left. This is aliasing: different expressions can access the same object. Reassigning p later changes only p. It does not redirect q. Writing through q still changes left. Copying the pointer did not copy the target object or give it another lifetime.

Explore with a visual model

Follow two pointers and their targets

VISUAL MODEL

Copy a pointer, redirect one copy, then write through each pointer. The arrows show which object each value refers to.

Start with int left = 4; and int right = 8; inside a function.

  1. int *p = &left;
  2. int *q = p;
  3. p = &right;
  4. *q = 6;
  5. *p = 9;
Object · intleft4
Object · intright8

left and right are initialised. The initializers for pointer objects p and q have not been reached yet.

left and right remain alive and writable throughout. The arrows describe relationships, while actual pointer representations depend on the implementation.

Read the complete trace
  1. left and right are initialised. The initializers for pointer objects p and q have not been reached yet.
  2. p receives a pointer to left. This does not copy left into p; p holds a pointer value.
  3. q receives a copy of p’s pointer value. Both now refer to left.
  4. p now refers to right. q still refers to left because q keeps its own copied pointer value.
  5. Writing 6 through q changes left. q itself still refers to left.
  6. Writing 9 through p changes right. Trace the pointer’s current target before deciding which integer changes.

3. Pointer parameters are still passed by value

Complete C17 program
#include <assert.h>

static void set_to_seven(int *destination)
{
    *destination = 7;
}

int main(void)
{
    int score = 4;
    set_to_seven(&score);
    assert(score == 7);
    return 0;
}

The function receives a copy of the address, then uses that copied pointer to designate the caller’s live object. C still passes the argument value by value; the target can change because both sides can access the same object. Reassigning the parameter itself would not reassign the caller’s pointer. A pointer-to-pointer can be used when an interface deliberately needs to update a caller’s pointer object.

This example’s contract requires a valid writable int target. A null check can be part of a different interface, but it is not a substitute for a lifetime, bounds, or type contract. State what the function accepts instead of implying every non-null pointer is usable.

4. Null means no object or function target

A null pointer compares unequal to pointers to objects or functions. In C17, an integer constant expression with value zero, or such an expression cast to void *, is a null pointer constant; NULL is a macro supplied by headers such as <stddef.h>. The source-level zero convention does not require an all-zero pointer representation or hardware address zero.

C17 · illustrative fragment
int value = 4;
int *optional = NULL;
assert(optional == NULL);
optional = &value;
if (optional != NULL) {
    *optional = 8;
}

A non-null value can still be stale, outside an allowed array range, misaligned, or unsuitable for the type of access. An uninitialized automatic pointer is not a usable “random address.” Initialize it, and ensure the target is valid for each operation. C23 adds nullptr and nullptr_t; it does not make null dereference valid.

5. Type and permission still matter

const int *view restricts changing an integer through that view; int *const fixed makes the pointer object nonmodifiable after initialization. Those declarations protect different things. Neither grants ownership or extends a lifetime. A converted object pointer can be held in void * and converted back as specified; standard C does not allow void * arithmetic or direct dereference of a void value.

Function pointers are their own topic; do not assume an object pointer or an int * can safely represent a function. Do not assume all pointer types have the same size. Start with correctly typed pointers to live objects, then study the specific conversion rules when an interface needs them.

Primary references and further reading

A pointer, its value, and its target

Compare, follow, and explain this idea

A pointer object stores a pointer value. A valid value can let an expression refer to another object. Changing the stored pointer value and changing the target are different actions.

When this helps Use this distinction when reading pointer assignments, shared access to an object, and functions that receive pointers.

Read the ideas side by side

Pointer object
The variable p has its own storage and a pointer type.
Pointed-to object
The int object left has separate storage and its own value.

Changing p does not, by itself, change left.

p = &right
Store a different pointer value in p.
*p = 6
Store 6 in the int object currently designated through p.

Read the left side first: which object is being changed?

Copy a pointer value
int *q = p gives q the value currently stored in p.
Copy a target value
int saved = *p reads an int value into a separate int object.

Neither operation makes q a live link to later changes of p.

Follow the reasoning

All objects below remain alive. Follow the named objects, without inventing numeric memory addresses.

C17 · illustrative fragments in the stated context

  1. int left = 4, right = 8;
  2. int *p = &left;
  3. int *q = p;
  4. p = &right;
  5. *q = 6;
  1. Create two int objects

    left starts at 4 and right starts at 8. Each object has its own stored value.

  2. Initialize p

    &left supplies a pointer to left. The declaration gives p that value.

  3. Initialize q from p

    q receives a copy of p’s current pointer value. Both values point to left.

  4. Redirect p

    p = &right changes p. q still points to left because q was not assigned again.

  5. Write through q

    *q = 6 changes left to 6. right remains 8; p points to right and q points to left.

Common misconceptions

These statements are misleading. Open each one to see why.

MisconceptionA copied pointer follows the original pointer variable.

The value is copied at that moment. To refer to the pointer object p itself, another pointer would need an appropriate pointer-to-pointer type.

MisconceptionA non-null pointer is always safe to dereference.

Non-null only excludes a null value. The target must still be alive, correctly aligned, and accessible through the expression’s type.

MisconceptionA cast repairs an invalid pointer.

A cast requests a type conversion. It does not create a live target object or grant access outside valid storage.

Go a little deeperOptional technical detail

A useful picture has limits

A box and arrow diagram shows a relationship. C does not require every pointer to be a simple numeric RAM address, nor every pointer type to share one size.

Pointer arithmetic has a separate contract

For object pointers, moving within an array or to its one-past position follows array rules. A one-past pointer cannot be used to access an element there.

C17 A null pointer can be represented using a null pointer constant in a suitable pointer context. Do not infer that every byte of a null pointer must be zero.

C23 C23 adds the null pointer constant nullptr; <stddef.h> names its distinct type nullptr_t. Conversion to a pointer type produces a null pointer. Lifetime, bounds, alignment, and access rules still apply.

Check the source rules

Public committee drafts; clause references help you verify the explanation. N3096 is a C23 working draft.

Say it in your own words

After p is redirected, why does writing through q still change left?

Compare with an explanation

q holds its own copied pointer value. Redirecting p does not assign a new value to q.

Before you move on

  • A pointer object stores a pointer value and is separate from its target.
  • Copying a pointer copies a relationship without copying or extending the target.
  • Valid access needs the right target, lifetime, range, type, and permissions.
Keep these ideas separate
Reassigning p / Writing through p
p = &other assigns a pointer value to p; *p = value writes to its current valid target.
Explain it in your own words

After q copies p, what changes when p is redirected, and what happens if you then write through q?

Optional: say it aloud or keep a note. You can continue whenever you are ready.

Check your understanding

Optional. Answers are not saved or sent, and reading stays unrestricted.

After q = p;, p is redirected to another live object. What happens to q?
Read the explanation for every choice
  1. q automatically follows the new p.

    The assignment copied a pointer value, not an ongoing dependency on p.

  2. q retains its earlier pointer value unless separately changed.

    ✓ Correct reasoning. The pointer objects are separate even when their values refer to the same target.

  3. The old target is freed automatically.

    Redirecting a pointer does not itself release the old target’s storage.

C17

Quick reference from the earlier edition

Retained for existing section links and saved passages. Read the expanded explanation above for the connected learning sequence.

Pointers are arguably C's signature feature — and its most notorious source of bugs.

Fundamentals

The address-of operator & yields a variable's memory address; the dereference operator * accesses the value stored at that address:

C17 · illustrative fragment
int x = 10;
int *p = &x;   /* p holds the address of x */
*p = 20;       /* x is now 20 */

Pointer Arithmetic

Pointer arithmetic advances in elements: p + 1 on an int * advances by sizeof(int) bytes, not a universally fixed four bytes. The original and resulting pointers must stay within the same array object or one position past its end; a one-past pointer must not be dereferenced. Pointer subtraction also requires the same array (or its one-past position) and a difference representable as ptrdiff_t. arr[i] means *(arr + i) for a valid index.

Pointers and Arrays

Most array expressions convert to a pointer to their first element, but an array is not itself a pointer. Important exceptions include an array operand of sizeof or unary &, and a string literal initializing an array. Inside a function, an adjusted array parameter is a pointer, so sizeof(parameter) gives the pointer size. Pass a separate element count where needed.

Multiple Indirection

int **pp = &p; is a pointer to a pointer. It can be used for an output parameter that changes a caller's pointer or for an array of separately managed row pointers. A true two-dimensional array such as int grid[3][4] normally converts to int (*)[4], a pointer to a row, not int **; the layouts and pointer types are different.

void Pointers

void * can hold a converted pointer to any object type. It cannot be directly dereferenced to access an object value; first convert it to an appropriate object-pointer type. In C, that conversion may be implicit: int *p = malloc(sizeof *p); needs no cast when <stdlib.h> is included. C does not generally guarantee conversions between object pointers and function pointers, and standard C does not permit arithmetic on void *.

NULL, Dangling, and Wild Pointers

  • A null pointer compares unequal to a pointer to any object or function. Use NULL from an appropriate header (or nullptr in C23); a null pointer is not guaranteed to have all-zero bits or identify hardware address zero. Dereferencing it is undefined behavior.
  • A dangling pointer refers to an object whose lifetime has ended. Under the C rules, a pointer value becomes indeterminate when the pointed-to object's lifetime ends; do not dereference, compare, or otherwise rely on a stale value. Assigning NULL to one pointer after free does not repair other aliases.
  • An uninitialized automatic pointer (often called a wild pointer) has an indeterminate value, not a usable “random address.” Initialize it before any read or use. Static-storage pointers without an explicit initializer are instead initialized to null pointers.

All three are common sources of the segmentation faults and memory-corruption bugs that make careful pointer discipline essential to reliable C programming.

Ready for the next idea?

Mark this chapter as read whenever you feel ready.

CHAPTER 23 / 73

In this chapter

Understanding pointers

Search all 73 chapters in both languages.EN + HG

Try “pointer”, “why int”, or “undefined behavior”.

Concepts connect. Follow your curiosity.↑ ↓ to explore · Enter to open
MAKE YOURSELF COMFORTABLE

Your reading space

Appearance

A clear canvas with balanced contrast.

Text size

100%
AA

Reading tools

Your data stays in this browser

Export a backup before changing browsers or replacing an older version of the reader.

YOUR PERSONAL MARGINS

My notebook

Saved on this device
A LITTLE CLARITY

The glossary

A starting meaning, then a chapter to go deeper.

A QUICK DEFINITION

A little clarity

Read the full explanation →

A thought worth keeping