Update a total
Read and write the same variable through a pointer. An easy embedded C problem (A first look at pointers), run and graded in the browser.
easy · A first look at pointers · pointers
Add amount to the variable that total points to.
Starter code
void add_to(int *total, int amount) {
}Example tests
Add a positive amount
int total = 10;
add_to(&total, 5);
printf("%d\n", total);Expected output:
15Add a negative amount
int total = 10;
add_to(&total, -4);
printf("%d\n", total);Expected output:
6Learn 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
- Read through a pointer · Follow an address to the value stored there.
- Write through a pointer · Change a variable that belongs to the caller.
- 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.