Pass a pointer to an integer
In this example we pass a pointer to a function so inside the function we'll be able to change the content of the variable in the caller.
#include<stdio.h>
void increment(int *x) {
*x = *x + 1;
}
int main() {
int counter = 0;
printf("Counter: %d\n", counter);
increment(&counter);
printf("Counter: %d\n", counter);
increment(&counter);
printf("Counter: %d\n", counter);
return 0;
}
Counter: 0
Counter: 1
Counter: 2