pointers-memory · intermediate · ~5 min

Dereference twice

Follow two levels of indirection.

Challenge

Follow two levels of indirection: a pointer to a pointer to an int, down to the int itself.

Task

Implement int deref2(int **pp) that returns the int reached by dereferencing pp twice (**pp). No main — the grader calls it.

Input

pp — a pointer to an int*, which in turn points to a valid int.

Output

Returns the int value at **pp.

Example

int x = 5; int *p = &x;
deref2(&p)   ->   5
x = 99;
deref2(&p)   ->   99

Why this matters

Drill: practice one contract before combining it with other skills.

Input format

pp — a pointer to an int*, which in turn points to a valid int.

Output format

Returns the int value at **pp.

Constraints

pp and *pp are non-NULL and point to live objects; **pp is a readable int. Do not modify either pointer or the int.

Starter code

int deref2(int **pp) {
    /* TODO */
    return 0;
}

Common mistakes

Following only one level and confusing an int pointer with an int value.

Edge cases to handle

Zero, negative and extreme int values; pointer redirection between calls.

Up next

Solve this exercise in the browser editor — compile and run against the test harness, no setup required.