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

标签 c arrays compiler-errors linked-list

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

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/

相关文章:

php - 使用另一个表组合三个表输出以进行交叉引用

python - 使用 zip 和 np.insert 将零部分插入 numpy 数组

python - 通过索引在矩阵中赋值

c++ - 错误c2248命名空间内的 friend 类

c++ - 创建我的第一个 vector 时出错 [Visual studio 2013]

c++ - 在C语言中使用遗传算法求一个数的平方根时如何实现选择和交叉

c - 在 for 循环中添加 { - } : c programming

c - 在命令行上终止 C 程序,但确保编写器已完成编写

angular - 在哪里可以找到我的 Angular 2 CLI TypeScript 错误?

c - 我如何控制其他程序?