← Fundamentals
Fundamentals

Pointers and dereferencing

What does a pointer value refer to, and what happens when you follow it?

Trace each level of access from a pointer or borrow to the selected value.

Outcome

Make and follow pointers, tell a write through a pointer from a change to the pointer, reach struct fields through a pointer, check for nil, None or nullptr before following, and follow a chain of pointers to its end.

Terminology (5)Terms used in this lesson

Term on the front, definition on the back. Paste with tabs between fields and a new line between cards, or import the download in Anki as Basic.

Pointer
A value that holds where another value is.
Dereference
Follow a pointer to the value it points to, written *p.
Address-of
Make a pointer to a variable, written &x.
Null pointer
A pointer without a pointee: nil in Go or nullptr in C++. The safe Rust examples represent an optional borrow with Option<&ConfigNode>.
Linked chain
Values joined by pointers, each one pointing to the next.

Making and following an access path

The & operator takes the address of a variable, producing a pointer such as *int. Dereferencing with * reaches the pointed-to value. The pointer itself is a value too, so identify whether an assignment targets the pointer or its referent.

A pointer can also be nil, which represents no target rather than a zero-valued target. Nested pointers add another access level. Trace each level before reading a field or making a write.

01& and *

Reach the selected retry budget

A dependency-config inspector lets you raise a selected node’s retry budget, report dirty targets and follow fallback records. Each view raises a different access question: which value receives a write, whether a target exists, and which record the next link selects. Retry budgets belong to this application’s 0..5 schema.

The inspector keeps a copied budget for comparison and selects the live budget for an edit. Trace the shown write and predict the values it reports.

main.go
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
package main

import "fmt"

func main() {
	budget := 2
	selected := &budget
	copied := budget

	*selected = 4

	fmt.Println(budget, copied, *selected)
}
What is printed?
Go · terminal.sh
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
go run main.go

Save one complete example at a time in a fresh folder using the selected filename. Run its shown build/check command before execution. Inspect the supplied program and predict its result before opening the explanation.

02Fields through a pointer

Edit a field of the selected node

The parser node has several fields. This probe selects the node and writes its retry field twice. Predict the value retained by parser.

main.go
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
package main

import "fmt"

type ConfigNode struct {
	Key     string
	Retries int
}

func main() {
	parser := ConfigNode{Key: "parser", Retries: 1}
	node := &parser

	node.Retries = 3
	(*node).Retries++

	fmt.Println(parser.Key, parser.Retries)
}
What is printed?

03Pointing nowhere

Distinguish an absent record from zero-valued fields

The index slot has no node. The budget helper is called for both a present parser and that missing selection. Predict what the helper reports or what happens when it reaches the missing selection.

main.go
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
package main

import "fmt"

type ConfigNode struct {
	Key     string
	Retries int
}

func budget(node *ConfigNode) int {
	return node.Retries
}

func main() {
	parser := &ConfigNode{Key: "parser", Retries: 3}
	var index *ConfigNode

	fmt.Println(budget(parser))
	fmt.Println(index == nil)
	fmt.Println(budget(index))
}
What happens?

04Pointers to pointers

Follow access to a selection, then to its value

The inspector can also hold access to the selected pointer itself. Trace the two access levels in the shown native and predict which budget or selection changes.

main.go
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
package main

import "fmt"

func main() {
	budget := 1
	selected := &budget
	selection := &selected

	**selection = 5
	other := 2
	*selection = &other
	*selected = 4

	fmt.Println(budget, other, **selection)
}
What is printed?
Loading Each dereference follows one layer…

05Repair the configuration inspector

Retry budgets, dirty targets, then fallback resolution

Repair the dependency-configuration inspector. Raise a node’s retry budget under the application schema, list dirty targets across optional slots, then resolve fallback chains to a terminal configuration.

The supplied caller owns each view’s retained records. ConfigNode holds retry settings, Target is a target-list record that may be absent from a slot, and ConfigLink describes configuration fallback resolution. A retry budget is an application setting bounded at 5; it is not a universal build-system rule. A slot can have no target, and a fallback chain can end without a resolved configuration.

Inputs are finite and well formed, with at most 1,000 short records and values fitting the shown integer types. In the fallback view, names are unique lowercase ASCII identifiers of 1..32 letters and exclude resolved and -. Every other fallback target names a supplied record. Those chains are acyclic and end at a resolved terminal or missing fallback. These are supplied assumptions; cycle detection and input validation are not assigned.

Repair the configuration inspector

Update the selected budgets, report targets across optional slots, then resolve each record’s fallback path.

  1. easy · F-PTR-V4-1 Raise the retry budgets
  2. medium · F-PTR-V4-2 Find the dirty targets
  3. hard · F-PTR-V4-3 Resolve fallback configurations
Sources

Report a problem in this lesson

For typos, mistakes and things that don’t work. We read every report and fix what’s wrong, but we don’t reply here, so a question won’t get an answer.