LearnFrenzy
Sign in Take a free test

Operators and Expressions · Find the Error · Question 6

Bug: *count++ instead of (*count)++ Answer, explanation & discussion

  • Find the error
  • Hard
  • 0 comments
  • Answer explained
  • Moderated community
Question 6 Back to Exercise

The function should add 1 to the caller's counter three times. What does the program print, and why?

main.c
#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!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links