pointers-memory · beginner · ~15 min

Set a value through a pointer

Write through a pointer with the dereference operator.

Challenge

Write a value into a variable that lives in the caller, given only a pointer to it.

Task

Implement void set_via_ptr(int *p, int v) that stores v into the int that p points to. No main — the grader calls it.

Input

p — a pointer to a caller int. v — the value to store.

Output

No return value. After the call, the int at *p equals v.

Example

int x = 0; set_via_ptr(&x, 42)   ->   x == 42

Input format

p (pointer to an int) and the value v.

Output format

No return value; *p is set to v.

Starter code

void set_via_ptr(int *p, int v) {
    /* TODO */
}

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