在C中声明不稳定

时间:2013-01-10 08:56:02

标签: c string pointers

我正在尝试编写棋盘游戏“Mastermind”(作业)的代码。我差不多写完了主要部分。不幸的是,我有一个不稳定的if语句,我无法用Netbeans调试解释。

在以下代码中,第58行if声明并不总是有效。游戏是这样的:

  1. 用户输入想要“随机”生成的颜色数
  2. 用户试图猜测(破坏代码)颜色。
  3. 如果N = 4(例如),则用户输入4种颜色的序列以试图猜测它们。 4.程序检查他是否正确并给出反馈意见。
  4. 6种颜色为:W:白色,B:蓝色,G:绿色,Y:黄色,P:紫色,R:红色。

    以下代码只是整体的一部分。请原谅我使用获取和声明此部分中未使用的变量。还有一些“愚蠢”的部分,但它们将在代码工作后更改它应该:)你能帮我调试第58行的if声明吗? (你会在旁边看到评论)。

    以下是代码:

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <time.h>
    
    char *colour(char *xr, int N);
    int RandomInteger(int low, int high);
    
    int main()
    {
        int i,j,q,N,w,z,k,A[6][4],win;
        char *xr,*xrx,temp[1],sur[]="SURRENDER";
    
        printf("Gimme number from 1-100");
        scanf("%d",&N);
        getchar();
    
        for(i=0; i<6; i++) A[i][1]=0;
        for(i=0; i<6; i++) A[i][2]=0;
        for(i=0; i<6; i++) A[i][3]=0;
        A[0][0]=87,A[1][0]=66,A[2][0]=71,A[3][0]=89,A[4][0]=80,A[5][0]=82;
        xr=malloc(N*sizeof(char));
        xrx=malloc(N*sizeof(char));
        xr=colour(xr,N);
    
        printf("\nxr: %s",xr);
        i=0;
        q=0;
        z=1;
        printf("\nGimme the color sequence: ");
        gets(xrx);
        //if(strcmp(xrx,sur)==0) goto end;
    
        while(xrx[i])
        {
       if(xrx[i]!=87 && xrx[i]!=66 && xrx[i]!=71 && xrx[i]!=89 && xrx[i]!=80 && xrx[i]!=82)               
           {z=0;}
           if(z==0) 
           {
               printf("\nGimme the color sequence: ");
               gets(xrx);
               i=0; 
               z=1;
           }
           else i++;
        }
    
        i=k=q=0;
        while(xr[i])
        {
        if(xr[i]==xrx[i]) 
        {
                q++;
                for(w=0; w<=5; w++)
                {
                         if(xr[i]==A[w][0]) //SOMETIMES IT DOESN'T WORK
                         {
                                 printf("\nhere:");
                                 A[w][3]=1;
                                 printf("%d",A[w][3]);
                                 break;
                         }
                } 
        }
        i++;
        }
        printf("\nColor and position correct: %d",q);
        for(i=0; i<=5; i++) printf("\n%d",A[i][3]); //PERIERGO
    
    
       // end:
       printf("!!BYE!!");
       return 0;
    }
    
    char *colour(char *xr, int N)
    {
        int r,i,j,number;
        char *str,temp[2];
    
        str=calloc(N,sizeof(char));
        srand((int)time(NULL));
        for(j=0; j<N; j++)
        {
           r=RandomInteger(0,5);
           if(r==0)
           {
               temp[0]='W';
               temp[1]='\0';
               strcat(str,temp);
           }
           else if(r==1)
           {
           temp[0]='B';
           temp[1]='\0';
           strcat(str,temp);
           }
           else if(r==2)
           {
               temp[0]='G';
               temp[1]='\0';
               strcat(str,temp);
           }
           else if(r==3)
           {
               temp[0]='Y';
               temp[1]='\0';
               strcat(str,temp);
           }
           else if(r==4)
           {
               temp[0]='P';
               temp[1]='\0';
               strcat(str,temp);
           }
           else if(r==5)
           {
               temp[0]='R';
               temp[1]='\0';
               strcat(str,temp);
           }
        }
        return str;
    }
    
    int RandomInteger(int low, int high)
    {
        int k;
        double d;
        d=(double)rand()/((double)RAND_MAX+1);
        k=(int)(d*(high-low+1));
        return (low+k);
    }
    

1 个答案:

答案 0 :(得分:4)

这是超出数组边界的访问,导致未定义的行为:

for(i=0; i<5; i++) A[i][3]=0;

A的尺寸为[5][3]。数组索引从0运行到N - 1,其中N是数组中元素的数量。还有其他出现的越界访问:

A[5][0]=82;

并在函数color()中:

char *str,temp[1];
str=malloc(N*sizeof(char)); /*Note sizeof(char) is guaranteed to be 1: omit.*/

/* snip */
temp[1]='\0';

str需要更大一个(不确定为什么temp甚至存在)。

不使用循环将数组A中的所有元素设置为零,而是使用聚合初始值设定项:

int A[5][3] = { { 0 } };

请参阅Why is the gets function so dangerous that it should not be used?