【发布时间】:2019-11-11 02:44:22
【问题描述】:
我是递归数组的初学者,所以需要一些指导。 我试图找出一个元素是否存在于数组中。
// Program to find whether an element exist in an array or not.
#include <stdio.h>
int arr[5]= {1,2,3,4,5};
int fooSearch(int array1[],int N,int i, int X)
{
if(i==N)
return 0;
else if (array1[i]==X)
return 1;
else
return fooSearch(array1,N,i++,X);
}
// N denotes total size 5
// i counter that moves from 0 to 4 and eliminate recursion when it reaches 5
// X is the element to be found
int main() {
fooSearch(arr,5,0,3);
return 0;
}
我得到的错误是Segmentation Fault (SIGSEGV)。
请指导我使用此代码做错了什么。
【问题讨论】: