gpt4 book ai didi

c - 分段故障核心转储

转载 作者:太空宇宙 更新时间:2023-11-04 06:29:32 25 4
gpt4 key购买 nike

#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);

我说这样更好,因为认为修改值很重要是一种误导;递归的关键是递归调用将有它们自己的 an 副本,因此在父项中修改它们对子项没有影响。

关于c - 分段故障核心转储,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22153662/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com