I want to return the last in a stack. Something like the following:
Item* get_last_item()
{
if (item_stack_size == 0) {
// return <nothing> ?
} else {
return ItemStack[item_stack_size-1];
}
}
What is the suggested practice when returning the equivalent of a null
value if the stack is empty? Should this usually issue a hard error? Something like a value of (Item*) 0
, or what's the suggested practice for doing something like this? My first thought was to do something like this, but I'm not sure if there's a better way:
Item* get_last_item()
{
return (item_stack_size != 0) ? ItemStack[item_stack_size-1] : (void*) 0;
}
question from:https://stackoverflow.com/questions/65852550/returning-an-empty-item-in-a-c-function-call