C++ Pointers Quiz: Arrays, References and Const Practice Test
Test your C++ pointer skills with 15 questions and three programs on arrays and std::array, pointer arithmetic, references, const correctness and grids.
- Course: C++ study plan
- Module: Arrays, pointers and references
- Kind: Checkpoint — cleared at 70%
- Reading time: 25 min
- Runtime: Clang 18 · C++20
Checkpoint — Arrays, pointers and references is the checkpoint that closes the Arrays, pointers and references module: a graded quiz and whole-program exercises, passed at 70%.
Instructions
This checkpoint covers the whole module: built-in arrays and std::array, pointers and nullptr, pointer arithmetic and the [begin, end) convention, references and dangling, const correctness, and grids.
How it works. Fifteen questions and three programs. You need 70% on the questions and every program accepted to clear the module. You can retake it as often as you like; your best score counts.
Before you start, make sure you can answer these from memory:
- What does an array's name become when you pass it to a function, and what is lost?
- What is
p[i]in terms of*and+, and what doesq - pgive? - Which pointer may you form but never dereference?
- Name the three things a reference cannot do that a pointer can.
- What is the difference between
const int*andint* const? - Where does cell
(r, c)of anR × Cgrid live in a flat vector? - Why is a reference into a
std::vectorunsafe afterpush_back?
The three programs are a pointer walk over runs of equal values, a flood count on a character grid, and a statistics function with const-correct reference parameters. Print values, indices and differences — never an address.
Common questions
What does an array's name become when you pass it to a function?
It decays to a pointer to the array's first element, and the size is lost: the parameter is just a T*, whatever the brackets say. Pass the length separately, pass a [begin, end) pair of pointers, or use std::array or std::vector, which keep their size.
Which pointer may you form but never dereference?
The one-past-the-end pointer, a + N for an array of N elements. It may be formed, compared and subtracted, and it marks the end of the range [a, a + N), but reading *(a + N) is undefined behaviour.
Why is a reference into a std::vector unsafe after push_back?
push_back may reallocate: when the capacity runs out, the vector moves its elements to a new, larger block and frees the old one. Any reference, pointer or iterator into the old block then dangles. Re-read the element afterwards, or hold an index, which survives.
Where does cell (r, c) of an R × C grid live in a flat vector?
At index r * C + c. Row-major order stores row 0's C elements first, then row 1's, so reaching row r skips r full rows before moving c further along.
Exercises
Runs through a pointer walk
Read n (1 ≤ n ≤ 1000) integers into a std::vector<int>. Walk the range [begin, end) with const int* pointers and print each maximal run of equal consecutive values as <value>x<length>, space-separated on one line. Write const int* run_end(const int* p, const int* end) that returns the pointer one past the run starting at p; the length of a run is a pointer difference.
Input: n, then n integers. Output: one line of runs.
Example: input 6 1 1 2 2 2 3 → output 1x2 2x3 3x1
Region size
Read R and C (1 ≤ R, C ≤ 50), R rows of . (open) and # (wall), then a start cell r c. Print the number of open cells reachable from the start by moving up, down, left or right through open cells, counting the start itself; if the start is a wall, print region=0. Use a std::vector<std::pair<int, int>> as an explicit stack, a seen grid, direction arrays and a bounds check before every access.
Input: R C, R rows, then r c. Output: region=<count>.
Example: input
3 4
..#.
.#..
..#.
0 0
→ region=5
Analyse and normalise
Read n (1 ≤ n ≤ 100) integers into a std::vector<int>. Write void analyse(const std::vector<int>& values, int& lo, int& hi, double& mean) — a const reference in, three references out — and void normalise(std::vector<int>& values, int shift) that subtracts shift from every element. Print the minimum, maximum and mean (two decimals), then the values after subtracting the minimum so that the smallest becomes 0.
Input: n, then n integers. Output: min=<lo> max=<hi> mean=<mean>, then the normalised values on one line.
Example: input 4 3 8 1 4 →
min=1 max=8 mean=4.00
2 7 0 3In this module: Arrays, pointers and references
- Built-in arrays and std::array
- Pointers — addresses, nullptr and the arrow
- Pointer arithmetic and arrays
- References — an alias with a rulebook
- Const correctness — reading declarations right to left
- Multidimensional data — grids, layout and neighbours
- Checkpoint — Arrays, pointers and references (this lesson)
← Multidimensional data — grids, layout and neighbours · The memory model — where objects live and when they die →