c - C 中的 if 语句不稳定

标签 c string pointers

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

在下面的代码中,第 58 行if 语句并不总是有效。游戏是这样的:

  1. 用户输入想要“随机”生成的颜色数
  2. 用户尝试猜测(破译)颜色。
  3. 如果 N=4(例如),则用户输入 4 种颜色的序列以尝试猜测它们。 4.程序检查他是否正确并给予反馈。

这 6 种颜色是:W:白色,B:蓝色,G:绿色,Y:黄色,P:紫色和 R:红色。

以下代码只是整体的一部分。请原谅我使用 gets 和声明这部分未使用的变量。还有一些“愚蠢”的部分,但它们会在代码正常运行后进行更改:) 你能帮我调试一下 第 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);
}

最佳答案

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

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

因为 A 具有维度 [5][3]。数组索引从 0N - 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?

关于c - C 中的 if 语句不稳定,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14253993/

相关文章:

c - 用正确的整数类型替换数组访问变量

c - execvpe 隐式声明错误

c - 如何从文本文件中写入和读取(包括空格)

c++ - 我需要在共享内存对象上使用 shm_unlink 吗?

regex - 将字符串中单词的首字母大写

pointers - Go 指针、引用、取消引用的规则 :

c - 指针关闭后,无法使用system()命令删除该文件,并显示该文件已被其他程序使用

python - 从反斜杠到斜杠(创建以数字命名的文件夹)

Swift 4/Xcode 9.3/iOS - 捕获文件 "x"无法打开,因为没有带有 String(contentsOf : url)

c - 在 C 中使用指针连接字符串!