开发者

How do I handle recursion and arrays in C?

开发者 https://www.devze.com 2023-02-10 10:52 出处:网络
So I have the following recursive function: int printSeq(int last[], int n, int arr[], int longest){ if(last[longest]==longest) return arr[longest];

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 locatio开发者_开发问答n.

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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消