0
我正在寫一個函數,它將採用已創建的鏈接列表並將該鏈接列表複製到另一個鏈接列表中。這是我迄今爲止複製鏈接列表到另一個鏈接列表
/* defined types:
AirportCode is an alias for a 4-char array
Node is a linked list node (struct) for AirportCodes */
typedef char AirportCode[4];
typedef struct node {
AirportCode airport;
struct node *next;
} Node;
Node *copy(Node *list) {
/* REPLACE THIS NON-SOLUTION */
struct node *temp = (struct node *)malloc(sizeof(struct node));
temp->airport = list->airport;
temp->next = copy(list->next);
return(temp);
return NULL;
}
/* test copy, and print the copy */
list3 = copy(list1);
printf("list3 copied from list1: "); printList(list3);
當我嘗試編譯這個使用gcc我得到的錯誤:
airports.c:50:19: error: array type 'AirportCode' (aka 'char [4]') is not
assignable
temp->airport = list->airport;
~~~~~~~~~~~~~^
1 error generated.
任何想法?
'memcpy(temp-> airport,list-> airport,sizeof(AirportCode))' – michaelmeyer