[英]Why is my recursive program giving me a segmentation fault?
當我運行我的程序時,我不斷收到分段錯誤。 當程序嘗試訪問計算機無法物理尋址的內存時,通常應該會發生分段錯誤。 我無法確定問題出在哪里。
編輯:我更改了掃描變量時添加了 & 但這並不能解決分段錯誤的問題
這是我的代碼:
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
void userEnter(int*pattern, int n);
void print( int * s, int n);
void recurs( int * s, int * a, int n, int wpegs, int bpegs);
bool Done (int*s);
bool bPegs(int*a ,int*s, int bpegs, int wpegs, int n);
bool wPegs(int* modcom, int* modoriginal, int*s, int wpegs, int w);
void change(int*modoriginal, int*modcom, int i, int k, int w);
int main(void)
{
int i, n, bpegs, wpegs;
printf("Enter the pattern length: ");
scanf("%d",&n);
int *a = (int*)malloc((n)*(sizeof(int)));
printf("Input the guess pattern: ");
int pattern[n];
userEnter(pattern, n);
printf("Enter the number of black pegs in the feedback: ");
scanf("%d",&bpegs);
printf("Enter the number of white pegs in the feedback: ");
scanf("%d",&wpegs);
printf("The possible key patterns are: ");
for(i=0; i<=n-1; i++)
{
a[i]=0;
}
print(a, n);
recurs(a, pattern, n, wpegs, bpegs);
}
void userEnter(int*pattern, int n)
{
char input[n];
scanf("%s",&input);
int i;
for(i = 0; i < n-1; i++)
{
pattern[i] = input[i]-65;
}
}
void print( int * s, int n)
{
int i;
printf( "\n" );
for( i = n-1; i >= 0; i-- )
{
printf( "%c", ( s[ i ] + 65 ) );
}
}
void recurs( int * s, int * a, int n, int wpegs, int bpegs)
{
int i;
if(Done(s))
{
print( s, n);
printf( "\nAccomplisshed!\n" );
}
else{
s[ 0 ] += 1;
for( i = 0; i < n-1; i++ )
{
if( s[ i ] == 6 ){
s[ i ] = 0;
s[ i + 1 ] += 1;
}
}
if(bPegs(a ,s, bpegs, wpegs, n))
{
print( s, n);
}
recurs(s, a, n, wpegs, bpegs);
}
}
bool Done (int*s)
{
int i;
bool done=true;
for (i=0;i<=11;i++)
{
if(s[i]!=5)
{
done=false;
}
}
return done;
}
bool bPegs(int*a ,int*s, int bpegs, int wpegs, int n)
{
int i,j,c=0;
bool d = false;
for(i=0; i<n-1; i++)
{
if(a[i]==s[i])
{
c++;
}
}
int x =n-c;
int* modcom;
int*modoriginal;
modcom=(int*)malloc((x)*(sizeof(int)));
modoriginal=(int*)malloc((x)*(sizeof(int)));
int w=0;
for(j=0; j<n-1; j++)
{
if(a[j]!=s[j])
{
modcom[w]=s[j];
modoriginal[w]=a[j];
w++;
}
}
if(c==bpegs)
{
d = wPegs(modcom, modoriginal, s, wpegs, w);
}
return d;
}
bool wPegs(int*modcom, int*modoriginal, int*s, int wpegs, int w)
{
int i, k, count=0;
for(i=0; i<=w; i++)
{
for(k=0; k<=w; k++)
{
if (modoriginal[i]==modcom[k])
{
count++;
change(modoriginal, modcom, i, k, w);
}
}
}
if(wpegs==count)
{
return true;
}
else
{
return false;
}
}
void change(int*modoriginal, int*modcom, int i, int k, int w)
{
int c, o;
for(c=i-1; c<w-1; c++)
{
modoriginal[c]=modoriginal[c+1];
}
for(o=k-1;o<w-1;o++)
{
modcom[o]=modcom[o+1];
}
}
因為您沒有正確地將參數傳遞給scanf
,正如編譯器所報告的那樣:
13421173.c:25: warning: format ‘%d’ expects type ‘int *’, but argument 2 has type ‘int’
13421173.c:25: warning: format ‘%d’ expects type ‘int *’, but argument 2 has type ‘int’
13421173.c:27: warning: format ‘%d’ expects type ‘int *’, but argument 2 has type ‘int’
13421173.c:27: warning: format ‘%d’ expects type ‘int *’, but argument 2 has type ‘int’
正確的用法如下:
scanf("%d", &bpegs);
我還沒有檢查所有的代碼,但你應該改變
scanf("%d",bpegs);
printf("Enter the number of white pegs in the feedback: ");
scanf("%d",wpegs);
到
scanf("%d",&bpegs);
printf("Enter the number of white pegs in the feedback: ");
scanf("%d",&wpegs);
即傳遞指向您希望 scanf 寫入的整數的指針
scanf
的參數是一個格式和一個指向相同類型格式的變量的指針。 對於整數%d
需要 &d ,其中 d 是int
類型。 對於字符串,如函數userEnter()
輸入, %s
需要一個類型char*
,輸入是一個數組,這意味着沒有大括號的輸入已經是一個指針,所以你只能寫
scanf("%s",input);
此外,您應該檢查您for
cicle 中的限制。 例如,在bPegs()
您正在為大小為x = n - c 的modcom 和 modoriginal 分配內存,而在下一個 cicle 中,您的限制為 n-1,除非 c = 1,否則這會產生分段錯誤。
就像是:
scanf("%d",bpegs);
進行從 int 到 int 指針的隱式轉換,以便讀取的整數將寫入某個隨機地址。該隨機地址取決於未初始化的 bpegs 的值。如果您多次使用 scanf,那么您有糾正所有這些錯誤,並傳遞要更改的值的地址,而不是值。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.