Thuta Learning
AdvancedProgrammingbeginner

Pointers

Relax. We'll talk through this in plain words — no textbook voice.

A pointer is a variable that stores a memory address. A big chunk of C's power comes from understanding pointers and memory. You'll need pointers when you want to change a value from inside a function, handle arrays/strings efficiently, or work with dynamic memory.

c
#include <stdio.h>

int main() {
  int age = 43;
  int *ptr = &age;

  printf("Address: %p
", (void *)ptr);
  printf("Value: %d
", *ptr);

  *ptr = 44;
  printf("New age: %d", age);
  return 0;
}

ptr stores the address of age. *ptr reads or changes the value at the location the pointer points to.

You should see
Address: 0x7ffe5367e044 Value: 43 New age: 44

Info

* can mean two different things depending on context: in a declaration it marks something as a pointer, while in an expression it dereferences one.

Easy traps

  • Don't dereference a pointer that hasn't been initialized — you'll end up touching random memory, which can crash your program.