fprintf无法正确显示值

时间:2016-03-07 16:39:50

标签: c

我正在尝试代表一个战卡游戏:

typedef struct list_of_cards* p_loc;
typedef struct game game_s;

struct list_of_cards {
    char card[4]; // card is represented like 10C or KS
    p_loc ncards; // next card
};

struct game {
    p_loc loc; // list of card
    p_loc lcard; // last card
}; 

p_loc init_cards(p_loc loc, int n) {
    loc = malloc(sizeof(struct list_of_cards));
    scanf("%s", loc->card);
    fprintf(stderr, "%s\n", loc->card);
    if (n == 1)
        return loc;
    init_cards(loc->ncards, n-1);
}

int main()
{
    int n; // the number of cards for player 1
    scanf("%d", &n);
    game_s player1;// the n cards of player 1
    player1.lcard = init_cards(player1.loc, n);
    fprintf(stderr, "%s %s\n", player1.loc->card, player1.lcard->card);
}

第一个fprintf给我我想要的东西(比如AD KC QC),但第二个显示

  

( QC

。 所以我的问题是,为什么 ( 是player1.loc->卡值。 感谢

2 个答案:

答案 0 :(得分:2)

您必须返回分配卡并将其分配回其前任的成员p_loc。此外,您需要为卡组的最后一张卡输出参数:

p_loc init_cards( int n, p_loc *lastCard )
                            // ^ output parameter
{
    if ( n <= 0 )                        // terminate recursion
        return NULL;                     // successor of last card is NULL

    p_loc loc = malloc(sizeof(struct list_of_cards)); // allocate new card
    if ( loc != NULL )
    {
        scanf("%s", loc->card);
        fprintf(stderr, "%s\n", loc->card);

        *lastCard = loc;                 // new card is last card
        loc->ncards = init_cards( n-1 ); // next card is successor of act card
    }
    return loc;                          // return new card
}

int main()
{
    int n; // the number of cards for player 1
    scanf("%d", &n);
    game_s player1;// the n cards of player 1
    player1.lcard = NULL;
    player1.loc   = init_cards( n, &player1.lcard );
    fprintf(stderr, "%s %s\n", player1.loc->card, player1.lcard->card);
}

但是你可以在没有递归的情况下轻松做到这一点:

p_loc init_cards( int n, p_loc *lastCard )
{
    *lastCard = NULL;
    p_loc head;
    p_loc *act = &head; // init pointer to first card
    for ( int i = 0; i < n; i ++ )
    {
        *act = malloc( sizeof(struct list_of_cards) ); // allocate next card right to target
        if ( *act == NULL )
            break;

        scanf( "%s", (*act)->card );
        fprintf( stderr, "%s\n", (*act)->card );

        *lastCard = *act;         // last card is act card
        act = &((*act)->ncards);  // set pointer to member ncards of last card
    }
    *act = NULL;                  // successor of last card is NULL
    return head;                  // return first card
}

答案 1 :(得分:2)

player1.loc-&gt; card之所以是一些奇怪的字符串,是因为它未初始化且内存中包含的随机字符在ascii中并不总是可打印的。 您需要修改init中的loc才能将loc作为指针传递给列表的第一个元素,但最好的方法是将整个播放器结构初始化为一个功能

p_loc init_cards(p_loc* loc, int n) {
    *loc = malloc(sizeof(struct list_of_cards));
    scanf("%s", (*loc)->card);
    fprintf(stderr, "%s\n", (*loc)->card);
    if (n == 1)
        {
            (*loc)->ncards = NULL;
            return *loc;
        }
    return init_cards(&((*loc)->ncards), n-1);
}

int main()
{
    int n; // the number of cards for player 1
    scanf("%d", &n);
    game_s player1;// the n cards of player 1
    player1.lcard = init_cards(&player1.loc, n);
    fprintf(stderr, "%s %s\n", player1.loc->card, player1.lcard->card);
}