元素打印显示意外值

时间:2016-01-05 12:17:49

标签: c

这是我的代码。它调用了一些函数,但没有任何与此问题相关的函数。

int main()
{



    srand((unsigned int)time(NULL)); //initializing srand

    struct card *deckAr = createDeck(); //creating the struct card deck array

    for (int i = 0; i < 100000; i++)
    {
        shuffleDeck(deckAr);
    }



        struct card *player1hand = (struct card*)malloc(sizeof(player1hand));
        struct card *player2hand = (struct card*)malloc(sizeof(player2hand));
        struct card *househand = (struct card*)malloc(sizeof(househand));

        player1hand = (struct card*)realloc(player1hand, sizeof(player1hand) * 2);
        player1hand[0] = deckAr[0];
        player1hand[1] = deckAr[1];

        printf("Card 1 %s of %s\n\n", valueName(player1hand[0].suit), suitName(player1hand[0].suit));
        printf("Card 2 %s of %s\n\n", valueName(player1hand[1].suit), suitName(player1hand[1].suit));
        printf("%s of %s\n", valueName(deckAr[0].value), suitName(deckAr[0].suit));


    return 0;
}

输出:

Card 1 Three of Hearts

Card 2 Three of Hearts

Ten of Hearts

由于没有任何操作deckAr,deckAr [0]不应该与player1hand [0]相同吗?

1 个答案:

答案 0 :(得分:3)

你打印这个:

valueName(player1hand[0].suit)

但我认为你的意思是:

valueName(player1hand[0].value)