During the greedy problem in pset1 I wrote this function:
void greedy(int coin_type, int *change, int *coins) {
while (*change / coin_type > 0) {
*coins++;
*change -= coin_type;
}
}
but when compiling I'd receive the error:
gabriel_greedy.c:32:9: error: expression result unused
[-Werror,-Wunused-value]
*coins++;
^~~~~~~~
So when I asked a fellow student what constitutes 'using' a variable, they told me that 'having it as part of a condition test in the function body qualifies as using that variable'.
I then went ahead and added this nonsensical but theoretically valid condition test:
void greedy(int coin_type, int *change, int *coins) {
while (*change / coin_type > 0 && *coins != -1) {
*coins++;
*change -= coin_type;
}
}
But still I received the same error:
gabriel_greedy.c:32:9: error: expression result unused
[-Werror,-Wunused-value]
*coins++;
^~~~~~~~
However, when I instead did:
void greedy(int coin_type, int *change, int *coins) {
while (*change / coin_type > 0) {
*coins += 1;
*change -= coin_type;
if (*coins != -1) {
continue;
}
the program compiled successfully. Now can someone please explain to me:
- What constitutes 'using' a variable in a function, and why does the compiler complain in the first case, second case but not in the last case?
- How is the third version of the function qualitatively different from the second version as it relates to 'using' the variable *coins, and compiling successfully or not?