【发布时间】:2019-11-15 10:04:27
【问题描述】:
我正在尝试创建一个链接列表并将值“Peter”保存在第一个列表元素中。我遇到了 char 数组的问题。我不能在其中插入“彼得”。
#include <iostream>
#include <string>
#include <stdlib.h>
using namespace std;
struct ListElement{
char name[20];
ListElement* next;
};
int main ()
{
ListElement* first;
first = new ListElement;
first -> name= "Peter"; // Array type 'char [20]' is not assignable
};
【问题讨论】:
-
改用
strcpy。 -
你不能分配给一个数组,只能复制到它。使用
strcpy(如前所述)或使用std::string。 -
你为什么
#include <string>然后就不用了?
标签: c++ arrays pointers linked-list