Adding a node of struct

This is a discussion on Adding a node of struct within the C Programming forums, part of the General Programming Boards category; I'm trying to make a linked list, what's wrong with my syntax for adding a node? Code: struct data { ...

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    31

    Adding a node of struct

    I'm trying to make a linked list, what's wrong with my syntax for adding a node?

    Code:
    struct data {
         char name[20];   
         char number[10]; 
    };
    
    typedef struct node {
         struct data list;
         struct node* next;
    } Node;
    
    Node *head = 0;
    Node *tail = 0;
    Node *New = 0;
    
    void add(char* name, char* number) {
        New = (Node *) malloc(sizeof(Node));
        New->list.name = name;   /* incompatible type assignment, from char* to char[20] */
        New->list.number) = number;   /* same error */
        New->next = 0;
    
        if (tail != 0) {
            tail->next = New;
            tail = New;
        }
        else {
            tail = New;
            head = New;
        }
    }

  2. #2
    Registered User
    Join Date
    Apr 2008
    Posts
    395
    as told by your compiler, you cannot directly set an array from another array, use strcpy() or memcpy() instead (besides, name is only a pointer in your function, the assignment makes no sense as its size is not known).
    Last edited by root4; 12-03-2008 at 06:11 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unknown memory leak with linked lists...
    By RaDeuX in forum C Programming
    Replies: 6
    Last Post: 12-07-2008, 04:09 AM
  2. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM
  3. Menu Help
    By CJ7Mudrover in forum C Programming
    Replies: 7
    Last Post: 03-09-2004, 09:59 AM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 02:27 PM
  5. Problem with Link List
    By Unregistered in forum C Programming
    Replies: 10
    Last Post: 04-25-2002, 12:49 AM

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