ctf Lesson 9 20 min read

CTF: Pointers

10 challenges — Week 6

Pointers Challenges

10 challenges Week 6 Week 6 (Pointers — The Heart of C)

Pointer challenges covering declaration and dereferencing, address-of operator, pass-by-pointer, pointer arithmetic, array-pointer equivalence, double pointers, const qualifiers, and common pointer bugs. These exercises build the foundational mental model students need for dynamic memory (Week 7), structs (Week 8), and systems programming.


Difficulty Breakdown

Level Count
Beginner 3
Intermediate 4
Advanced 3

Challenges

1. Pointer Expressions

Difficulty: Beginner Time: ~10 min Type: expression evaluation

Trace the evaluation of the following pointer expressions. For each, give the resulting value and specify whether it is an integer value or a memory address.

Given: int x = 42; int y = 17; int *p = &x; int **pp = &p;

Concepts: pointer declaration with , dereference operator (), address-of operator (&), double pointer (int **pp), pointer comparison (== with addresses)

Practice on CodeStepByStep


2. Memory State Tracing

Difficulty: Beginner Time: ~12 min Type: assertion table

Given the following code, determine the values of all variables and what each pointer points to at each labeled point.

int a = 10, b = 20, c = 30; int *p1 = &a; int *p2 = &b; // Point A *p1 = 50; p2 = &c; *p2 = *p1 + 5; // Point B p1 = p2; *p1 = 0; // Point C

Concepts: pointer initialization with &, modifying values through dereference (*p = value), reassigning pointers (p = &other), pointer aliasing (two pointers to same variable), distinguishing *p = val from p = &val

Practice on CodeStepByStep


3. Swap Function

Difficulty: Beginner Time: ~8 min Type: function

The following swap function doesn’t work. Explain why, then write a corrected version using pointers.

Buggy code: void swap(int a, int b) { int temp = a; a = b; b = temp; }

int main(void) { int x = 5, y = 10; swap(x, y); printf(“x=%d y=%d\n”, x, y); // prints x=5 y=10 (…

Concepts: pass-by-value vs pass-by-pointer, using pointers as function parameters, dereference to read and write through pointers, address-of operator (&) when calling functions, why C does not have pass-by-reference

Practice on CodeStepByStep


4. Output Parameters

Difficulty: Intermediate Time: ~12 min Type: function

Write a function void findMinMax(int arr[], int size, int *min, int *max) that finds both the minimum and maximum values in an array and stores them through the pointer parameters.

Since C functions can only return one value, pointer parameters (“output parameters”) are the standard way to retur…

Concepts: output parameters (returning multiple values via pointers), writing through dereferenced pointers (*min = value), array traversal with pointer outputs, address-of operator (&) in function calls, C’s single-return limitation and the pointer workaround

Practice on CodeStepByStep


5. Pointer Arithmetic

Difficulty: Intermediate Time: ~10 min Type: expression evaluation

Given the declarations below, evaluate each pointer arithmetic expression. Assume a 64-bit system where sizeof(int) == 4 and sizeof(int*) == 8.

int arr[5] = {10, 20, 30, 40, 50}; int *p = arr;

Concepts: pointer arithmetic scales by element size, subscript operator as syntactic sugar: p[i] == *(p + i), pointer subtraction yields element count, negative array indices via pointer arithmetic, sizeof(array) vs sizeof(pointer) distinction

Practice on CodeStepByStep


6. Array Pointer Equivalence

Difficulty: Intermediate Time: ~8 min Type: expression evaluation

This challenge explores the relationship between arrays and pointers in C.

Given: int data[4] = {100, 200, 300, 400};

Part A: Rewrite the following array-notation expressions using pointer arithmetic, and vice versa. Part B: Evaluate both forms and verify they produce the same result. Part C: E…

Concepts: array-pointer equivalence: arr[i] == *(arr + i), commutativity of addition makes i[arr] valid, array name decay to pointer in expressions, arrays are NOT pointers: arr is not a modifiable lvalue, pointer reassignment vs array immutability

Practice on CodeStepByStep


7. Pointer Bug Hunt

Difficulty: Advanced Time: ~15 min Type: bare code

Each of the following code snippets contains exactly one pointer bug. For each:

  1. Identify the bug
  2. Classify it (NULL dereference, dangling pointer, type mismatch, or uninitialized pointer)
  3. Explain the danger (what could happen at runtime)
  4. Write corrected code

Concepts: NULL pointer dereference (CWE-476), dangling pointer / return of stack address (CWE-562), type mismatch: int vs int* (CWE-843), uninitialized pointer use (CWE-824), defensive programming with pointers

Practice on CodeStepByStep


8. Double Pointer Tracing

Difficulty: Advanced Time: ~12 min Type: expression evaluation

Trace through the following code and predict the output of each printf statement.

int x = 5, y = 10; int *p = &x; int **pp = &p;

printf(“x=%d\n”, x); printf(“*p=%d\n”, *p); printf(“**pp=%d\n”, **pp);

**pp = 20; printf(“x=%d\n”, x);

pp = &y; printf(“p=%d\n”, *p); printf(“**pp=%d\n”, **pp);

Concepts: double pointer declaration (int pp), two levels of dereference (pp), modifying values through double dereference (*pp = value), redirecting a pointer through its double pointer (pp = &other), tracing pointer chains after modifications

Practice on CodeStepByStep


9. Const Pointers

Difficulty: Intermediate Time: ~8 min Type: expression evaluation

For each of the three const-pointer declarations below, determine which operations are valid (compile successfully) and which cause a compile error.

int x = 10, y = 20;

Declaration 1: const int *p1 = &x; // pointer to const int Declaration 2: int * const p2 = &x; // const pointer to int…

Concepts: const int *p: pointer to const (protects data), int * const p: const pointer (protects pointer), const int * const p: both const (protects both), reading const declarations right-to-left (clockwise/spiral rule), const correctness in function signatures

Practice on CodeStepByStep


10. Comprehensive Pointer Tracing

Difficulty: Advanced Time: ~15 min Type: expression evaluation

This capstone challenge combines all pointer concepts from Week 6. Given the program below, trace through execution and determine the complete output.

#include

void update(int *a, int *b, int *result) { *result = *a + *b; *a = *a * 2; *b = 0; }

void processArray(int arr[]…

Concepts: pass-by-pointer with multiple output parameters, modifying caller variables through pointers in functions, order of operations within a function body, double pointer redirection (*pp = &y changes where p points), output parameter pattern (sum and max)

Practice on CodeStepByStep