Andre
Andre

Reputation: 33

How do I handle recursion and arrays in C?

So I have the following recursive function:

int printSeq(int last[], int n, int arr[], int longest){

     if(last[longest]==longest) return arr[longest];
     printf("%d ", printSeq(last, n, arr, last[longest]));
}

last is an array with locations pointing to array. Longest is the current location.

However when I run it I get strange values that are not in the array. Am I missing something?

The base case is when the last[longest] points to its own location

Upvotes: 2

Views: 256

Answers (1)

aschepler
aschepler

Reputation: 72271

The function doesn't return any value after a printf. This results in Undefined Behavior.

Upvotes: 10

Related Questions