在 C 中的结构元素上复制字符串

Copy a string on a structure element in C

我不明白为什么我的字符串没有被复制。

字符串结构类似于这个“KS 2H 5C JD TD”

这是我的代码(评论都是我试过的(memcpystrcpy)):

typedef struct Hand Hand;
struct Hand {
    char *cards;
    double power;
};
Hand* initHand(char *set){
    Hand *hand = malloc(sizeof(*hand));
    if(hand == NULL)
        exit(EXIT_FAILURE);

    char card[5][3];
    //strcpy(hand->cards,*cards);
    int i=0;
    char *p = strtok (set, " ");

    while (p != NULL)
    {
        card[i] = p;
        printf("%s\n",p);
        p = strtok (NULL, " ");
        i++;
    }

    hand->power=0;
}

我愿意用 strtok 复制数组中的每 2 个字母(这就是我想做的);然后我尝试复制整个字符串。

非常感谢。我明白了我的错误:

错误:

  • 我不知道 strtok 是一个破坏性函数(我试图编辑只读数据,因为我使用的是常量)。
  • 我没有为char *cards
  • 分配内存

解决方案:

  • 复制该集。
  • 分配内存。

这是对我有用的代码:

struct Hand {
    char cards[5][3];
    double power;
};


Hand* initHand(char *set){
    Hand *hand = malloc(sizeof(*hand));
    if(hand == NULL)
        exit(EXIT_FAILURE);

    char copy_set[15]="";
    strcpy(copy_set,set);

    char **str = (char**) malloc(5*sizeof(char*));
    for(int i=0; i<3; i++){
        str[i]= (char) malloc(3*sizeof(char));
    }
    strcpy(hand->cards,str);

    int i=0;
    char *p = strtok (copy_set, " ");

    while (p != NULL)
    {
        strcpy(hand->cards[i], p);
        p = strtok (NULL, " ");
        i++;
    }
    
    hand->power=0;
    return hand;
}

我认为这是相当正确的代码 — OP 对 answer 修订版 2 中显示的代码进行了相对简单的修改。当然,总有改进的余地。

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

struct Hand
{
    char cards[5][3];
    double power;
};

typedef struct Hand Hand;

extern Hand *initHand(const char *set);

Hand *initHand(const char *set)
{
    Hand *hand = malloc(sizeof(*hand));
    if (hand == NULL)
        exit(EXIT_FAILURE);

    char copy_set[15] = "";
    strcpy(copy_set, set);

    int i = 0;
    char *p = strtok(copy_set, " ");

    while (p != NULL)
    {
        strcpy(hand->cards[i++], p);
        p = strtok(NULL, " ");
    }

    hand->power = 0;
    return hand;
}

#include <stdio.h>

int main(void)
{
    Hand *hand = initHand("KS 2H 5C JD TD");

    for (int i = 0; i < 5; i++)
        printf("Card %d: [%s]\n", i, hand->cards[i]);
    printf("Power:  %.2f\n", hand->power);

    free(hand);
}

正如我在 中指出的那样,不需要变量 str。使用修改后的结构,无需分配额外的卡。我已将字符串输入标记为 const。可以说,最好为 copy_set 进行动态内存分配(记住也要释放副本),或者至少检查给定数据是否适合分配的 space。在内存分配失败单方面退出之前打印一条错误消息可能会更好。检查故障是很好的。退出时说出原因是不好的。失败时 return NULL 可能会更好,然后让调用代码检测并处理问题。

代码中也附有一个简单的测试程序。

谨慎使用strtok();在库函数中使用它是危险的。您通常应该优先使用 strtok_r() (POSIX) 或 strtok_s() (Microsoft)。