Simvik

Increment through a pointer

Fix a precedence bug that moves the pointer instead of the value. An easy embedded C problem (A first look at pointers), run and graded in the browser.

easy · A first look at pointers · pointers · precedence · debugging

This function should add one to the variable that counter points to. It compiles and runs, and the variable never changes. Fix it.

Starter code

void increment(int *counter) {
  *counter++;
}

Example tests

One call

int c = 0;
increment(&c);
printf("%d\n", c);

Expected output:

1

Two calls

int c = 0;
increment(&c);
increment(&c);
printf("%d\n", c);

Expected output:

2

Learn it first

More A first look at pointers problems