[英]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--);
}
不知道為什么它不起作用。
分段故障(核心轉儲)。
else
return Pair(a++ , n--);
使用后綴遞增和遞減運算符將導致遞歸調用處理相同的值。 您應該使用前綴運算符,或者甚至更好,只需加減1。
else
return Pair(a + 1 , n - 1);
我說那更好,因為認為修改價值觀很重要會產生誤導。 遞歸的關鍵是遞歸調用將擁有其自己的a
和n
副本,因此在父級中對其進行修改不會對子級產生影響。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.