Skip to content
AgileLogics
6 min read

Pointers: Introduction

Prerequisites: Function declarations & header files

A pointer is a variable that holds a memory address — the location of another value — rather than the value itself.

Syntax

int x = 10;
int *p = &x;   // p holds the ADDRESS of x

printf("%d\n", x);    // 10 — the value
printf("%p\n", (void *) p); // some address, e.g. 0x7ffee3a1c...
printf("%d\n", *p);   // 10 — DEREFERENCE p to get x's value

& ("address-of") gets a variable's address; * ("dereference"), used on a pointer, follows that address back to the value.

Real-world analogy: a pointer is like a house's street address written on a sticky note — the note itself is small, but it tells you exactly where to go to find the actual house (the value).

Example

void increment(int *p) {
    *p = *p + 1;   // modifies the ORIGINAL variable, through its address
}

int main(void) {
    int x = 5;
    increment(&x);
    printf("%d\n", x); // 6 — actually changed this time
    return 0;
}

Expected output

6

This is how C achieves what Functions couldn't — passing &x (x's address) lets increment modify the original x, not a disposable copy.

Common mistakes

  • Confusing * the declaration ("this variable is a pointer") with * the operator ("dereference this pointer") — they look identical but mean different things depending on context.
  • Dereferencing a pointer that was never assigned a valid address (a "wild pointer") — this is undefined behavior, and often crashes the program (a segmentation fault).

Practice

Write a function void doubleIt(int *p) that doubles the value p points to.

Show solution
void doubleIt(int *p) {
    *p = *p * 2;
}