I am trying to write a function that takes a linked list, an index and returns the value at the index+1 position.
int getData(list *l, int index) {
if(...) {
...
return result; // this could be -1
}
else {
return -1;
}
}
In C you return a value of -1 when the function fails (as a way to let the user know that the call failed because of bad input). However, because the return type of this function is int, it includes the value of -1 (-1 as a number, as well ass -2, -3 etc)
My question is: Is there a mechanism in which you can return from your function and make the user of the function aware that the returned value is in fact an error and not a value?
Return the value by passing a pointer and make the return value an error statement:
int getData(list *l, int inIndex, int* outIndex) {
if(...) {
...
*outIndex = result;
return 0;
}
else {
return -1;
}
}