Learn C With Me #04 — Pointers | Live

In Lesson 4 of Learn C With Me, we tackle one of the most infamous — and important — concepts in C programming: pointers.

Using Beej’s Guide to C as our study material, we start from the very beginning: how variables are stored in memory, what memory addresses actually represent, and how a pointer allows us to refer to data indirectly.

We cover:

  • Memory, variables and addresses
  • How data is stored in memory
  • A brief look at endianness
  • What a pointer actually is
  • The address-of operator (&)
  • Printing addresses with %p
  • Pointer types
  • Declaring and initializing pointers
  • Pointers to different data types
  • Dereferencing pointers with *
  • The difference between a pointer and the value it points to
  • Modifying variables through pointers
  • Passing pointers as function arguments
  • Using pointers to modify variables outside a function’s local scope
  • The NULL pointer
  • What happens when you dereference a null pointer
  • A brief look at nullptr in C23
  • Pointer declaration syntax and some of its potential pitfalls
  • Using sizeof with pointers
  • Pointers to pointers

Along the way, we experiment with the code ourselves and spend plenty of time trying to build an intuitive understanding of what pointers actually mean rather than simply memorizing the syntax.

We also see our first really practical reason for using pointers: overcoming C’s pass-by-value behavior by passing a variable’s address to a function, allowing that function to modify the original variable.

As usual, there are mistakes, questions, compiler warnings, a segmentation fault or two, and plenty of help from the live chat while we figure things out together.