0

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 :/

4
  • 3
    Should you not check what the recursive call to find_chap return? Commented Jan 19, 2017 at 10:39
  • Returning NULL if no entry iks found seems appropriate here. BTW you don't need recursion here. Commented Jan 19, 2017 at 10:41
  • You must use return find_chap(...); Commented Jan 19, 2017 at 10:52
  • Well I need a recursion because the entries of the ->list can also then have again an allocated ->list and so on... Commented Jan 19, 2017 at 10:53

2 Answers 2

1

You need to:

  • Record whether the current parameter ch matches your target
  • Loop over the ch child nodes until either you have a match to return (which you may already) or exhaust the list.
  • Return the result of the above.

In short, something like this:

CHAP* find_chap(CHAP* ch, CHAP* target)
{
    CHAP *res = (ch->ID == target->ID) ? ch : NULL;

    for (int i=0; !res && i<ch->nb; ++i)
        res = find_chap(ch->list[i], target);

    return res;
}

That should fulfill your requirement of having a single return point in the function, while still recursing children.

Sign up to request clarification or add additional context in comments.

3 Comments

Can you tell me what the !res in the loop is standing for. I have seen this notation before but i could not find anywhere an explanation. I am confused because res is a pointer and not a bool or integer value
@malajedala that's the test for whether res is still NULL or not. It is equivalent to (res == NULL). The loop will break as soon as (a) res is non-NULL, or (b) i has met ch->nb. Note that if res was already set to non-NULL on the first line because ch->ID == target->ID, then the loop effectively becomes a noop and no iterations take place; !res becomes false and therefore the for-condition is no longer meetable.
Oh I see. I just tried your example and it is exactly what I was trying to do. Thank you so much for your help and your explanations!
1

You forgot to return a value from the recursive call:

CHAP* find_chap(CHAP* ch, CHAP* target){

     if (ch->ID == target->ID) return ch;

     for (int i=0; i<ch->nb; i++){
         CHAP * c = find_chap(ch->list[i], target);
         if (c != NULL) return c;         // return not null value if one was found
     }
    return NULL;   // not found here...
 }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.