Skip to content
📖 Auto vs Modern CBook contents

Chapter 11: Pointers ​

Level 2 — Cognition

The most powerful and dangerous feature of C, and how Auto eliminates the danger.

Pointers are C's defining feature. They give direct access to memory, enable efficient data structures, and underlie every non-trivial C program. They are also the source of most C bugs: null dereferences, dangling pointers, buffer overflows, and use-after-free errors.

Auto eliminates raw pointers from user code while preserving the expressive power they provide. This chapter is a deep dive into C pointers so you understand what Auto handles for you.


11.1 Pointer Operations ​

A pointer is a variable that stores the address of another variable. C provides two fundamental pointer operators:

  • & (address-of): obtains the memory address of a variable
  • * (dereference): accesses the value at a memory address
c
// C Deep Dive: basic pointer operations
int x = 42;
int *ptr = &x;       // ptr holds the address of x
printf("Address: %p\n", (void*)ptr);
printf("Value: %d\n", *ptr);    // dereference: prints 42

*ptr = 99;            // modify x through the pointer
printf("x is now: %d\n", x);    // prints 99

Pointer arithmetic. Pointers to array elements support arithmetic:

c
// C Deep Dive: pointer arithmetic
int arr[5] = {10, 20, 30, 40, 50};
int *p = arr;         // p points to arr[0]

p += 3;               // p now points to arr[3]
printf("%d\n", *p);   // prints 40

int diff = p - arr;   // pointer subtraction: 3

The compiler multiplies the offset by sizeof(int), so p += 3 actually adds 3 * sizeof(int) bytes to the address. This is why pointer arithmetic requires a typed pointer -- the type determines the element size.

Auto's replacement. Auto has no pointer type. Array indexing replaces pointer arithmetic:

auto
let arr [5]int = [5]int{10, 20, 30, 40, 50}
print("arr[3]:", arr[3])   // direct indexing, no pointers

Released under the MIT License.