SO i am trying to write a recursive function, in which i only want to have "one" return.
So what I am trying to do is, first I have defined a simple recursive structure:
typedef struct CHAP CHAP;
struct CHAP{
int ID;
CHAP** list;
int nb;
};
Now in my function I want to find and return a pointer to CHAP if it has the same ID as my target. So far I have this:
CHAP* find_chap(CHAP* ch, CHAP* target){
if (ch->ID == target->ID) return ch;
for (int i=0; i<ch->nb; i++){
find_chap(ch->list[i], target);
}
//return NULL?
}
However I am not sure how to return something if the entry is not found? So in that case I would like to return NULL but I don't know where exactly to place it. I am not considering the case if the ID is contained at several entries, then only the first found pointer will be returned.
If I do it in the line where it is commented out, the function and recursion calls will continue even after a entry is found, which is not what i want :/
find_chapreturn?NULLif no entry iks found seems appropriate here. BTW you don't need recursion here.return find_chap(...);