为什么我的递归程序给我一个分段错误?

时间:2012-11-16 17:06:35

标签: c recursion 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];
        }
    }

4 个答案:

答案 0 :(得分:3)

因为您没有正确地将参数传递给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);

答案 1 :(得分:1)

我没有检查所有代码,但你应该改变

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写入

的整数

答案 2 :(得分:1)

scanf的参数是格式和指向同一格式的变量的指针。 对于整数%d requiers&amp; d,其中d是int类型。对于一个字符串,比如函数userEnter()中的输入,%s需要一个类型char*,输入是一个数组,这意味着没有大括号的输入已经是一个指针,所以你只能写

scanf("%s",input);

另外,您应该检查for cicles中的限制。例如在bPegs()中你为modcom和modoriginal分配内存 x = n - c ,在下一个cicle你的限制转到n-1,这会产生一个分段错误除非c = 1。

答案 3 :(得分:0)

类似的东西:

scanf("%d",bpegs);

从int到int指针进行隐式转换,以便读取整数将写入一个随机地址。这个随机地址取决于bpegs的值,它没有初始化。如果你使用scanf很多次,然后你必须纠正所有这些错误,并传递值的地址来改变,而不是值。

相关问题