problem with my program

This is a discussion on problem with my program within the C++ Programming forums, part of the General Programming Boards category; When ever I try to compile my program it gives me an error at tempNode->name = Name; saying "incompatible types ...

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    61

    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

  2. #2
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    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;
    }

  3. #3
    Registered User
    Join Date
    Aug 2001
    Posts
    61
    Thanks!

  4. #4
    Registered User
    Join Date
    Aug 2001
    Posts
    58
    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...
    --== www.NuclearWasteSite.com==--

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multi Thread Program Problem
    By ZNez in forum C Programming
    Replies: 1
    Last Post: 01-03-2009, 11:10 AM
  2. Program Termination Problem
    By dacbo in forum C Programming
    Replies: 3
    Last Post: 01-23-2006, 02:34 AM
  3. Inheritance and Dynamic Memory Program Problem
    By goron350 in forum C++ Programming
    Replies: 1
    Last Post: 07-02-2005, 03:38 PM
  4. Replies: 20
    Last Post: 06-13-2005, 12:53 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21