我试图用c编写一个简单的游戏http://pastebin.com/BxEBB7Z6。我们的目标是通过获得随机数来尽可能接近21来击败计算机。

每一轮都会显示球员的姓名和总和,但是由于某些原因,它只能在第一次使用时有效?像这样:

玩家John的总和为0。
玩家的总和为9。
玩家的总和为11。

等等。

为什么只显示一次玩家的名字,但之后却没有其他任何显示?我不在某处进行重新分配:-)

我使用功能void PrintPlayerSum(struct Player *p)将其打印出来,它第一次起作用,但仅此而已。

#include <stdio.h>
#include <stdlib.h>

#include <time.h>

struct Player
{
    char name[256];
    int sum;
};

void PrintPlayerSum(struct Player *p)
{
     printf("Player %s has sum %d\n", p->name, p->sum);
}

void wait ( int seconds )
{
    clock_t endwait;
    endwait = clock () + seconds * CLOCKS_PER_SEC ;
    while (clock() < endwait) {}
}

int main()
{
    struct Player *player = malloc(sizeof(*player));
    strcpy( player->name, "John");
    player->sum = 0;

    while(1)
    {
        PrintPlayerSum(player);

        printf("Do you want another number? (y/n, q for quit) ");
        char ch;

        scanf("%s", &ch);

        if( ch == 'q' )
            break;

        if( ch == 'y' )
        {
            srand(time(NULL));

            int rnd = rand() % 13 + 1;
            player->sum += rnd;

            printf("Player got %d\n", rnd);
        }

        if( ch == 'n' || player->sum > 21)
        {
            if( player->sum > 21 )
            {
                printf("\n*** You lost the game, please try again... ***");
            }
            else
            {
                printf("\nCPU's turn\n");

                int cpusum = 0;

                while( 1 )
                {
                       if( cpusum > 21 )
                       {
                           printf("\n*** CPU lost the game with the score %d, you win! ***", cpusum);
                           break;
                       }

                       if( cpusum > player->sum )
                       {
                           printf("\n*** CPU won the game with the score %d, please try again ***", cpusum);
                           break;
                       }

                       wait(1);
                       srand(time(NULL));
                       int rnd = rand() % 13 + 1;
                       cpusum += rnd;

                       printf("CPU got %d, sum is %d\n", rnd, cpusum);
                }
            }

            break;
        }

        printf("\n\n");
    }

    /* Cleanup ******************/
    free(player);
    /****************************/

    printf("\n\n\n");
    system("PAUSE");
    return 0;
}

最佳答案

我怀疑问题是您对scanf的使用。您说您想读取一个以零结尾的字符串,但是您将其填充到一个字符中。变量在堆栈上的布局方式会导致终止的零字节最终成为player-> name中的第一个字符。

尝试键入“缓冲区溢出”而不是“ y”,您应该得到“播放器缓冲区溢出...”。

如果要坚持使用scanf,则要确保将正确的字符串传递给它,并对目标缓冲区的大小设置限制。要读取一个字符,请尝试fgetc。

编辑:
上面的内容当然不是很正确……这是缓冲区溢出,但是它是播放器结构的指针被覆盖。通过幸运的巧合,您将获得一个指向零字节的有效地址。通过输入更多,您很可能会崩溃。

关于c - C程序中的内存错误,名称消失了吗?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/7510718/

10-11 23:21
查看更多