pointers-memory · beginner · ~4 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

Why this matters

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

Input format

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

Output format

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

Constraints

p points to a live writable int; p is never NULL. Write only that int. No allocation or ownership transfer.

Starter code

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

Common mistakes

Assigning to the local pointer rather than its target; changing a neighboring int.

Edge cases to handle

Zero, negative values, INT_MIN/INT_MAX, repeated writes, and a pointer into an array must work without changing neighbors.

Up next

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