The description uses list, not array, so I would use
Code:
struct enemy {
struct enemy *next;
int health;
int damage;
};
struct enemy *alive = NULL; /* No alive enemies yet */
struct enemy *dead = NULL; /* No dead enemies yet */
Because the enemies are in a singly-linked list, when damaging an enemy kills it, we also need a pointer to the list pointer to be able to kill the enemy. For example, if we have:
Code:
void kill_enemy(struct enemy **alive_list, struct enemy **dead_list, struct enemy *target)
{
/* If no target, do nothing. */
if (target == NULL)
return;
if (alive_list != NULL) {
/* Find target in alive list, and detach it. */
if (*alive_list == target) {
*alive_list = target->next;
target->next = NULL;
} else {
struct target *prev = *alive_list;
while (prev->next != target && prev->next != NULL)
prev = prev->next;
if (prev_next) {
prev->next = target->next;
target->next = NULL;
}
}
}
if (dead_list) {
/* Prepend target to dead list. Most recently deceased first. */
target->next = *dead_list;
*dead_list = target;
}
}
then we could kill a known target enemy foo using
Code:
kill_enemy(&alive, &dead, foo);
assuming the global lists are used in the first snippet.
The reason I didn't use the global lists directly in kill_enemy() is that this way you can have multiple alive and dead lists in a single program. It's not like providing a pointer to the list pointer is that annoying.