The hero has 50 HP and 2 potions. drink(h) uses one: a potion fewer and 30 more HP, but never above 100. With no potions it does nothing (already written). main drinks three times.
HP: 100, potions: 0
#include <stdio.h>
struct Hero {
int hp;
int potions;
};
void drink(struct Hero *h) {
if (h->potions == 0) {
return;
}
// your code here: one potion fewer, 30 more hp, and back to 100 if above
}
int main(void) {
struct Hero hero = {50, 2};
drink(&hero);
drink(&hero);
drink(&hero);
printf("HP: %d, potions: %d\n", hero.hp, hero.potions);
return 0;
}
Run tries the examples. When they look right, Submit.