Skip to content
Docs
English
Esc
↑↓navigate↵open⌘Jpreview
On this page

9. Pointers

Pointer declaration, dereferencing, arithmetic, and the relationship between arrays and pointers in C.

Pointers hold memory addresses. They are central to C’s power.

  • Declaration and dereferencing
  • Pointer arithmetic
  • Arrays and pointers
  • Pointers to pointers

Examples

Swap with Pointers (swap_pointers.c)

Demonstrates pass-by-reference using pointers to modify variables in the caller:

#include <stdio.h>

void swap(int *x, int *y) {
    int t = *x;
    *x = *y;
    *y = t;
}

int main(void) {
    int a, b;
    printf("Enter two integers (a b): ");
    scanf("%d %d", &a, &b);
    swap(&a, &b);
    printf("After swap: a=%d, b=%d\n", a, b);
    return 0;
}

Pointer Arithmetic (pointer_arithmetic.c)

Shows how incrementing and decrementing a pointer moves through array elements:

#include <stdio.h>

int main(void) {
    int a[] = {1,2,3};
    int *p = a;
    printf("*p=%d\n", *p);
    p++;
    printf("*p after ++ = %d\n", *p);
    p--;
    printf("*p after -- = %d\n", *p);
    return 0;
}

Was this page helpful?