如何使用strcpy将字符串数组存储到结构中?

时间:2014-03-21 19:41:03

标签: c string loops struct enums

我在Ubuntu中使用GCC制作一个小型ANSI C应用程序,需要使用strcpy()。

我的标题文件:

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

#define DECKSZ 52

typedef struct card {
    enum {ACE=1, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING} pips;
    enum {SPADES, CLUBS, HEARTS, DIAMONDS} suit;
    char cardName[20];
} card;

extern card deck[];

void initDeck(card[]);
void labelCards();
void shuffleDeck(card[]);
void swap(card*,card*);

我的主要档案:

#include "CardOps.h"

card deck[DECKSZ];

void initDeck(card deck[]) {
    int counter;
    for (counter = 0; counter < DECKSZ; counter++) {
        deck[counter].pips = (const)((counter % 13) + 1);
        deck[counter].suit = (const)(counter / 13);
    }
}

void labelCards(card deck[]) {
    static const char *pipNames[] = {"Ace","Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten","Jack","Queen","King"};
    static const char *suitNames[] = {"Spades","Hearts","Diamonds","Clubs"};
    int i;
    for (i = 0; i < DECKSZ; i++) {
        strcpy(deck[i].cardName, pipNames[i]);
        /*strcpy(cardName, suits[i]);*/
    }
}

int displayCards(card deck[], int numCards) {
    int i, countCards;
    if (numCards > 52)
        countCards = 52;
    else
        countCards = numCards;
    for (i = 0; i < countCards; i++) {
        printf(deck[i].cardName);
    }
    return countCards;
}

void shuffleDeck(card deck[]) {
    int i, j;
    for (i = 0; i < DECKSZ; i++) {
        j = rand() % DECKSZ;
        swap(&deck[i], &deck[j]);
    }
}

void SortCards() {

}

void swap(card *c1, card *c2) {
    card temp;
    temp = *c1;
    *c1 = *c2;
    *c2 = temp;
}

int main(void) {
    initDeck(deck);
    /*labelCards(deck);*/
    displayCards(deck,52);
    shuffleDeck(deck);
    return EXIT_SUCCESS;
}

我在labelCards()函数中使用strcpy()时遇到问题。有人请帮我strcpy()吗?谢谢!

2 个答案:

答案 0 :(得分:2)

我认为您使用sprintf比使用strcpy更好。这是因为sprintf返回一个整数,告诉您写入目标缓冲区的字符数。有了这些知识,你就会知道在你想写这套服装时从哪里开始写作。

 //copy the name into the buffer at cardname
int written = sprintf(deck[i].cardName, "%s" pipNames[i%13]);
 //copy the suit name into the same buffer, but advanced by however many chars we just wrote
sprintf(deck[i].cardName + written, "%s", suits[i/13]);

现在它会说像FiveHearts和AceClubs这样的东西......不是最佳的,但你可以在这里工作。

编辑: user3386109建议采用比我更聪明的方法:

sprintf(deck[i].cardName, "%s of %s", pipNames[i%13], suits[i/13] );

一个sprintf肯定比我正在做的两个好。在某些情况下,知道如何使用sprintf继续写下你离开的地方是有价值的,但这不是其中之一。我还根据用户3386109的修正案更正了pipNamessuits中索引的逻辑。

答案 1 :(得分:0)

在:

void labelCards(card deck[]) {
    static const char *pipNames[] = {"Ace","Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten","Jack","Queen","King"};
    static const char *suitNames[] = {"Spades","Hearts","Diamonds","Clubs"};
    int i;
    for (i = 0; i < DECKSZ; i++) {
        strcpy(deck[i].cardName, pipNames[i]);
        /*strcpy(cardName, suits[i]);*/
    }
}

pipNames[i]i > 12时访问不存在的数组元素。

您需要像pipNames[i % 13]一样initDeck。感觉标签应该在initDeck函数中。