【问题标题】:error: assignment to expression with array type in c错误:在 c 中赋值给具有数组类型的表达式
【发布时间】:2020-08-29 11:55:38
【问题描述】:

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

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

【问题讨论】:

  • card是一个数组,不能作为=运算符的LHS。
  • 我该如何解决它

标签: c arrays compiler-errors linked-list


【解决方案1】:

您必须逐个字符地复制字符串或使用 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;
} 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-11
    • 2020-12-20
    • 1970-01-01
    相关资源
    最近更新 更多