user3313082
user3313082

Reputation: 21

Segmentation fault core dump

#include <stdio.h>

void ScanArray (int* , int*);
int Pair (int* , int);

int main () {

        int a [15] , n;

        ScanArray (a , &n);

//      printf("\nHello World !!!\n");

        if(Pair(a , n) == 0)
                printf("The array fulfills the requirements");
        else
                printf("The array does not fulfills the requirements");

        return 0;
}

void ScanArray (int *a , int *n) {

        int i , f = 0;


        do {
        printf("Enter the number of integers : ");
        scanf("%d",n);

        if (*n > 15)
                printf("Enter number bellow 15 \n");
        else
                f=1;
        } while(f == 0);

        printf("Enter the %d integers : ",*n);
        for (i = 0 ; i < *n ; i++)
                scanf("%d",a+i);

}

int Pair (int *a , int n) {

        if (n <= 1)
                return 0;
        else
                if (*a-*(a+1) != 1 && *a-*(a+1) != -1)
                        return 1;
                else
                        return Pair(a++ , n--);

}

don't know why it's not working.

Segmentation fault (core dump).

Upvotes: 0

Views: 472

Answers (1)

John Kugelman
John Kugelman

Reputation: 362197

else
    return Pair(a++ , n--);

Using postfix increment and decrement operators will result in the recursive call processing the same values. You should either use prefix operators, or even better, just add and subtract 1.

else
    return Pair(a + 1 , n - 1);

I say that's better because it's misleading to think modifying the values matters; the key to recursion is that the recursive calls will have their own copies of a and n, so modifying them in a parent has no effect on the children.

Upvotes: 4

Related Questions