█
LastWrite
  • > Curriculum
  • > Pricing
  • > For Educators
  • > About
  • > Contact
Log InGet Started

Questions, concerns, bug reports, or suggestions? We read every message, write to us at [email protected].

More ways to reach us →
LastWrite

Structured computer science lessons for aspiring developers and security professionals.

[email protected]

(201) 785-7951

Mon–Fri, 9 AM–5 PM EST

Learn

  • Curriculum
  • Pricing

Company

  • About
  • For Educators & Schools
  • Contact Us

Legal

  • Terms of Service
  • Privacy Policy
© 2026 LastWrite. All rights reserved.
Curriculum/Programming Languages/C/Pointers
55 minIntermediate

Pointers

After this lesson, you will be able to: Use pointers: pointer arithmetic, pointers to arrays, pointers to functions, pass-by-reference idiom.

Pointers are the source of C's power and most of its bugs. Master them.

Prerequisites:Arrays and Strings

What a pointer is

An address in memory.

tsx
int x = 42;
int *p = &x; // p holds the address of x
printf("%d\n", *p); // 42, dereference; read what p points at
*p = 100; // write through p; x is now 100
printf("%d\n", x); // 100
// Null pointer
int *np = NULL;
if (np == NULL) printf("empty\n");
// *np would crash (segfault on most systems)

Pointers + arrays

Arrays decay to pointers when passed.

tsx
int nums[] = {10, 20, 30};
int *p = nums; // p points to nums[0]
printf("%d\n", *p); // 10
printf("%d\n", *(p+1)); // 20, pointer arithmetic
printf("%d\n", p[1]); // 20, same thing; nums[i] === *(nums + i)
// Pass to function, length must come separately
int sum(int *arr, size_t n) {
int total = 0;
for (size_t i = 0; i < n; i++) total += arr[i];
return total;
}
sum(nums, 3);

Pass-by-reference idiom

C is pass-by-value. To modify a caller's variable, pass its address.

tsx
// WRONG, sets the local copy
void set_zero_bad(int x) { x = 0; }
// RIGHT, modifies the caller's variable
void set_zero(int *x) { *x = 0; }
int main(void) {
int n = 5;
set_zero_bad(n); // n still 5
set_zero(&n); // n now 0
return 0;
}
// Same pattern for swap, returning multiple values, etc.
void swap(int *a, int *b) {
int tmp = *a;
*a = *b;
*b = tmp;
}

Function pointers

Pass behaviour like data, used by qsort.

tsx
#include <stdlib.h>
int compare_ints(const void *a, const void *b) {
int ai = *(const int *)a;
int bi = *(const int *)b;
return (ai > bi) - (ai < bi); // -1, 0, or 1
}
int main(void) {
int nums[] = {3, 1, 4, 1, 5, 9, 2, 6};
size_t n = sizeof(nums) / sizeof(nums[0]);
qsort(nums, n, sizeof(int), compare_ints);
return 0;
}

Common mistakes only experienced C devs avoid

Dereferencing NULL or uninitialized pointers. Pointer arithmetic on void*. Not allowed (size unknown). Returning a pointer to a stack local variable. Undefined behavior, the local is gone after return. Wild pointers (uninitialized). Always initialise to NULL.

Quick Check

Why is `int *get_local(void) { int x = 5; return &x; }` undefined behavior?

Pick the C-spec answer.

Sign in and purchase access to unlock this lesson.

Sign in to purchase
←Arrays and Strings
Back to C
Memory Management→