-
problem with my program
When ever I try to compile my program it gives me an error at tempNode->name = Name; saying
"incompatible types in assignment of `char *' to `char[20]'"
heres some of my code:
Code:
const int MAX = 20;
....................................
private:
struct node
{
char name[MAX];
int age;
node* nxt;
};
node* Root;
node* LastNode;
node* tempNode;
Data* Data_Ptr;
node* tempNode2;
............................................
void List::AddData(char Name[MAX], int Age)
{
tempNode = new node;
tempNode->name = Name;
tempNode->age = Age;
tempNode->nxt = NULL;
.................................................
Can anyone tell me why its doing this or how to fix it?
Thanks!
btw im using dev c++ as my compiler
-
You have to use strcpy or strncpy to copy the characters:
Code:
#include <cstring>
using namespace std;
// Code...
void List::AddData(char Name[MAX], int Age)
{
tempNode = new node;
strncpy(tempNode->name, Name, MAX - 1);
tempNode->name[MAX - 1] = '\0';
tempNode->age = Age;
tempNode->nxt = NULL;
}
-
-
Ahh....good old subject lines that are SO indiscrete that you have no clue what there about. "Problem with my program" is about as bad as "please help me". O well, we cant all be creative... :)