Read through a pointer
Follow an address to the value stored there. An easy embedded C problem (A first look at pointers), run and graded in the browser.
easy · A first look at pointers · pointers · dereference
A pointer holds the address of a variable, which is where that variable lives in memory. Putting * in front of a pointer means "the value at that address".
Return the value that p points to.
Starter code
int read_value(const int *p) {
return 0;
}Example tests
A positive value
int x = 42;
printf("%d\n", read_value(&x));Expected output:
42A negative value
int x = -7;
printf("%d\n", read_value(&x));Expected output:
-7Learn it first
- A first look at pointers · Addresses, and how a function reaches a variable that belongs to its caller.
More A first look at pointers problems
- Write through a pointer · Change a variable that belongs to the caller.
- Update a total · Read and write the same variable through a pointer.
- Increment through a pointer · Fix a precedence bug that moves the pointer instead of the value.
- Return two results · Use pointer parameters to hand back more than one value.
- Handle a missing value · Check a pointer before following it.
- Return a pointer · Hand back the address of an element, or NULL when there is none.