简体   繁体   English

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

[英]How can I use strcpy to store string arrays into a struct?

I am making a small ANSI C application using GCC in Ubuntu, which requires the use of strcpy(). 我在Ubuntu中使用GCC制作一个小型ANSI C应用程序,它需要使用strcpy()。

My header file: 我的头文件:

#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*);

My main file: 我的主要档案:

#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;
}

I am having trouble with getting strcpy() working within my labelCards() function. 我在labelCards()函数中使用strcpy()时遇到问题。 Would somebody please help me with strcpy()? 有人请帮我strcpy()吗? Thanks! 谢谢!

I think you'd be better off using sprintf than strcpy . 我认为使用sprintf比使用strcpy更好。 This is because sprintf returns an integer that tells you how many chars you wrote to the destination buffer. 这是因为sprintf返回一个整数,告诉你写入目标缓冲区的字符数。 With that knowledge you know where to start writing when you want to write the suit. 有了这些知识,你就知道在你想写西装时从哪里开始写作。

 //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]);

Now it will say things like FiveHearts, and AceClubs.... not optimal, but you can work from here. 现在它会说像FiveHearts和AceClubs这样的东西......不是最佳的,但你可以在这里工作。

Edit: user3386109 suggests an approach much smarter than mine: 编辑: user3386109建议一种比我更聪明的方法:

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

One sprintf is definitely better than the two I was doing. 一个sprintf肯定比我正在做的两个更好。 There are some cases where knowing how to continue writing where you left off with sprintf is valuable, but this is not one of them. 在某些情况下,知道如何继续使用sprintf从中断写作是有价值的,但这不是其中之一。 I've also corrected the logic for the indices in pipNames and suits according to user3386109's amendments. 我还根据user3386109的修正案更正了pipNamessuits索引的逻辑。

In: 在:

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] accesses a non-existing array element when i > 12 . i > 12时, pipNames[i]访问不存在的数组元素。

You need to do pipNames[i % 13] as you do in initDeck . 您需要像在initDeck那样执行pipNames[i % 13] It feels like the labeling should be in initDeck function. 感觉标签应该在initDeck函数中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM