Segmentation fault on recursive function
Hey guys, I am having segmentation fault on recursive function @ int fill_array(). If I enter number like 0 to 16100, the program works fine. For any integer > 16100, the program crashes.
I am running out of ideas what is the problem. Can someone enlighten me?
Code:
#include <stdio.h>
int palindrome(int start, int end, int count);
int fill_array(int arr[], int num, int start);
int check_array(int arr[], int num, int end);
int main(void)
{
int count = 0, start, end;
printf("Enter start and end: ");
scanf("%d %d", &start, &end);
count = palindrome(start, end, count);
printf("Number of palindrome numbers = %d\n", count);
return 0;
}
int palindrome(int start, int end, int count){
int array[9], counter;
if(start > end){
return count;
}else{
counter = fill_array(array, start, 0);
count += check_array(array, start, counter);
return palindrome(++start, end, count);
}
}
int fill_array(int arr[], int num, int start){
if(num != 0){
arr[start] = num % 10;
return fill_array(arr, num/10, ++start);
}else{
return start;
}
}
int check_array(int arr[], int num, int end){
if(end == 0){
return 1;
}else if(num % 10 != arr[end-1]){
return 0;
}else{
return check_array(arr, num/10, --end);
}
}
I try malloc array in palindrome() but it still won't works :(