CodeTheRobot

C++ Beginner · Lesson 10 of 22

Pointers and references: T*, T& and passing by reference

Addresses, dereferencing, nullptr, references, and const references.

Estimated time · 60–90 min

Learning goals

What you will understand

  • Explain pointers and references: t*, t& and passing by reference in your own words
  • Use it in a small robotics example
  • Verify the result instead of assuming it worked

Understand the idea first

The mental model

A pointer (T*) stores the address of a value and may be empty (nullptr). A reference (T&) is another name for an existing object and can never be empty or reseated. Use a reference for a required argument and a pointer only when absence is meaningful.

Passing by value copies; passing by reference lets the function change the original; passing by const reference avoids a copy without allowing changes. Never dereference a pointer before checking it is not null.

Guided lesson

Work through these steps

  1. 1.

    Read the pointers and references: t*, t& and passing by reference example and identify what each line owns or changes.

  2. 2.

    Create a small source file, compile with -std=c++17 and warnings enabled, and run it.

  3. 3.

    Change one input, predict the result before running, then compare the output.

  4. 4.

    Record one compiler or runtime error and explain the correction in your own words.

Worked example

Read it before you run it

void zero(double& value) { value = 0.0; }
// Takes a reference: writing to value changes the caller's variable.
double read_or(const double* sensor, double fallback) {
// Takes a pointer that may be null, promising not to modify what it points to.
  return sensor != nullptr ? *sensor : fallback;
// Dereference (*) only after the null check.
}
// End of read_or.
double joint{1.2};
// A normal variable.
zero(joint);
// joint is now 0.0, because the function received a reference.
double reading{3.4};
// A sensor value.
double a = read_or(&reading, -1.0);
// & takes the address of reading; the result is 3.4.
double b = read_or(nullptr, -1.0);
// No sensor: the fallback -1.0 is returned safely.

What the example is doing

  1. 1.Read declarations from right to left: identify each value's type, name, and initial value.
  2. 2.Follow the data through each expression before considering syntax shortcuts.
  3. 3.Compile with warnings enabled; a clean build is part of the result, not an optional polish step.

Watch it run · Values in memory

See what this code does

The terminal types this lesson's practice solution and runs it. The scene shows the kind of robot behaviour that code drives. It plays by itself and loops.

motor speed030x1070x1410x1890x1c40x2060x2420x2880x2c*p += 10VALUES IN MEMORY

Demo for Pointers and references: T*, T& and passing by reference. Expected result: 0 3.4 -1

Practice code

Now type it yourself

Copy the starter into practice.cpp and replace every TODO with your own code. Build and run it: g++ -std=c++17 -Wall -Wextra -Wpedantic practice.cpp -o practice -pthread && ./practice

#include <iostream>

// TODO: write zero(double& value) so it sets the caller's variable to 0.0

// TODO: write read_or(const double* sensor, double fallback): return *sensor, or fallback if sensor is nullptr

int main() {
  double joint{1.2};
  zero(joint);
  double reading{3.4};
  std::cout << joint << '\n' << read_or(&reading, -1.0) << '\n' << read_or(nullptr, -1.0) << '\n';
  return 0;
}

Expected output

0
3.4
-1
Stuck? Show the full solution (try it yourself first)
#include <iostream>

void zero(double& value) { value = 0.0; }

double read_or(const double* sensor, double fallback) {
  return sensor != nullptr ? *sensor : fallback;
}

int main() {
  double joint{1.2};
  zero(joint);
  double reading{3.4};
  std::cout << joint << '\n' << read_or(&reading, -1.0) << '\n' << read_or(nullptr, -1.0) << '\n';
  return 0;
}

Expected result

How you know it worked

  • After adding the shown fragment to the guided exercise with its listed headers and surrounding program, the finished program builds with -Wall -Wextra -Wpedantic.
  • Changing the stated input produces the result you predicted.

Practical exercise

Apply the lesson

Write swap_joints(double& a, double& b) and a function taking const std::vector<double>&. Then write one function that accepts a pointer and test both a valid pointer and nullptr.

Before you continue

  • The program builds with -std=c++17 and no warnings
  • You can explain the key rule without reading the example
  • The normal and edge cases both have recorded results

If something goes wrong

The compiler reports that a name or type is unknown.

Start at the first error. Check spelling, required #include lines, and whether the declaration appears before use.

The program runs but the value is wrong.

Print the inputs and intermediate values, then check conversions, units, and collection order.