C++ 如何在字符数组中插入值?

C++ 如何在字符数组中插入值?,c++,arrays,pointers,linked-list,C++,Arrays,Pointers,Linked List,我试图创建一个链表,并将值“Peter”保存在第一个列表元素中。我的字符数组有问题。我不能在里面插入“彼得” #include <iostream> #include <string> #include <stdlib.h> using namespace std; struct ListElement{ char name[20]; ListElement* next; }; in

我试图创建一个链表,并将值“Peter”保存在第一个列表元素中。我的字符数组有问题。我不能在里面插入“彼得”

    #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


   };
#包括
#包括
#包括
使用名称空间std;
结构列表元素{
字符名[20];
ListElement*下一步;
};
int main()
{
ListElement*首先;
第一个=新列表元素;
first->name=“Peter”//数组类型“char[20]”不可赋值
};

要设置
char[]
数组的值,应使用
strcpy
strncpy
函数,如:

strcpy(名字->名字,“彼得”);
但是,最好使用
std::string
类型,您可以直接指定:

struct列表元素{
std::字符串名;
ListElement*下一步;
};

然后
first->name=“Peter”是有效的代码。

请改为尝试
strcpy
。您不能分配给数组,只能复制到数组。使用(如前所述)或使用。为什么您要将其包含在内,然后不使用它?