gpt4 book ai didi

c - 错误: assignment to expression with array type in c

转载 作者:行者123 更新时间:2023-12-02 10:56:38 24 4
gpt4 key购买 nike

我正在使用此功能来创建一个链表,其中每个节点都是完整牌组中的一张牌,但是我遇到了这个奇怪的错误。有什么帮助吗?

typedef struct cards
{
char card[3];
struct cards * next;
} cards_t;

cards_t * make_list(char ** deck) //function to make list
{
int j = 0;
cards_t *head = malloc (sizeof(cards_t));

for (cards_t * iterator = head; j<52; iterator = iterator->next, j++)
{
iterator->card = deck[j];
iterator->next = malloc(sizeof(cards_t));
}

return head;
}

最佳答案

您必须通过char复制字符串char或使用string.h中的复制功能。

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

typedef struct cards
{
char card[3];
struct cards * next;
} cards_t;

cards_t * make_list(char ** deck) //function to make list
{
int j = 0;
cards_t *head = malloc (sizeof(cards_t));

for (cards_t * iterator = head; j<52; iterator = iterator->next, j++)
{
strcpy(iterator->card,deck[j]);
iterator->next = malloc(sizeof(cards_t));
}

return head;
}

关于c - 错误: assignment to expression with array type in c,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61772104/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com