I'm trying to create a linked list and save the value "Peter" in the first list element. I'm having a problem with the char array. I can't insert "Peter" in it.
#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
};
To set the value of a char[]
array, you should use the strcpy
or strncpy
function, like:
strcpy(first->name, "Peter");
But, better would be to use the std::string
type, which you can directly assign:
struct ListElement{
std::string name;
ListElement* next;
};
Then first->name = "Peter";
is valid code.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments