-
Linked List Problem
Hi,
When I try to insert a name into my linked list I get output that looks something like: ==============&=== and I don't know why.
Here is a simple version of my code which is causing my problems:
Code:
#include <iostream.h>
struct node
{
char name[20];
node *next;
};
int main()
{
node * L= new node;
char newname[20];
cout<<"Enter a name:"<<endl;
cin >> newname;
L->name[20] = newname[20];
cout<<L->name;
return(0);
}
Any reasons why this is happening?
Thanks in advance
-
strcpy owns you
Code:
#include <iostream.h>
#include <string.h>
struct node
{
char name[20];
node *next;
};
int main()
{
node * L= new node;
char newname[20];
cout<<"Enter a name:"<<endl;
cin >> newname;
strcpy (L->name, newname);
cout<<L->name;
return(0);
}
-
and when copying arrays, no need for [] operator.