Question 6
Back to Exercise
The function should add 1 to the caller's counter three times. What does the program print, and why?
#include <stdio.h>
void tick(int *count) {
*count++;
}
int main(void) {
int clicks = 0;
tick(&clicks);
tick(&clicks);
tick(&clicks);
printf("%d\n", clicks);
return 0;
}
- A 3
- B 0 – postfix ++ binds tighter than *, so *count++ moves the pointer and leaves the counter unchanged Correct
- C 1
- D It crashes
Correct Answer: Option B
*count++ is *(count++): it increments the local pointer and reads the old target, throwing the value away (compilers warn "expression result unused"). Write (*count)++ or ++*count.
Discussion & Comments 0
Share your method, a shortcut, or ask about a step you didn't follow.
No comments yet. Be the first to share your solution!