Since you use info->error
to say if the function failed or not, you can return whatever you want, since the caller should ignore the return value. So you can silent the warning with return -1
, return 0
, return MAGIC_NUMBER
... ...
In general however function are coded in the "opposite way": the return value says if the function succeeded or not. If all int return values are good, you can write the function so that it returns failure or success, and on success you fill the data. In your case your info struct could hold a int data
, or you can add another arg to the func.
This way the caller can do something like
if ( pop(stack, info) == SUCCESS ) {
// ...
printf("%d\n", info->data);
} else { /* failure */
// info->data holds no data, but info->error could be an error code, e.g.
fprintf(stderr, "can't pop: %s\n", error_msg[info->error]);
}
The usage in your case is less intuitive:
data = pop(stack, info);
if (info->error != ERROR) {
// data is valid
} else {
// data is garbage and we have to say an error occurred.
}
BTW, you do not set info->error
to something different by 0, so your code is potentially bugged; e.g.
info->error = 0;
data = pop(stack, info);
would trigger always an error even though indeed stack is ok and so data is valid.