Thread: Linked list question modfiying element contents

  1. #1
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335

    Linked list question modfiying element contents

    I'm having trouble modifying the contents of a linked list element. Regular variables such as double, int, char are no problem:

    temp->whateverthevariableisinthestruct = newvalue;

    But i'm having problems with a char array that makes a customer name.

    temp->customerName = customerName;



    customerName;

    is the new value (array) that i want to set and customerName is the variable in the struct.

    It doesn't seem to compile

    201 operands of = have illegal types 'array 30 of char' and 'pointer to char'

  2. #2
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    a sample code of writting/reading data into the list
    Code:
    #include<stdio.h>
    #include<string.h>
    
    struct node
    {
        char name[25];
        struct node *link;
    };
    
    struct node * getnode()
    {
        return malloc(sizeof(struct node));
    }
    
    void clear_buffer(void)
    {
        int ch;
        
        while((ch=getchar())!='\n' && ch!=EOF);
    }
    struct node* insertnode(struct node *h)
    {
        struct node *p;
        int data;
        char temp[25];
        
        if(h==NULL)
        {
            h=getnode();
            h->link=NULL;
            p=h;
        }
        else
        {    
            p=getnode();
            p->link=h;
        }
        printf("Enter the name\n?");
        clear_buffer();   
        fgets(temp,sizeof(temp),stdin);   // fgets(p->name,sizeof(p->name),stdin) (better way of doing)
        strcpy(p->name,temp);   
        h=p;
      return h;
    }
    
    displaynodes(struct node *h)
    {
        while(h!=NULL)
        {
            printf("%s\n",h->name);
            h=h->link;
        }
    }
    
    int main()
    {
        struct node *h;
        int choice,i,search,del;
      h=NULL;
        
        printf("1. Insert an element\n");
        printf("2. Display elements\n");
        printf("3. Exit \n");
        
         printf("\nEnter your choice\n");
         scanf("%d",&choice);
         
         while(choice!=3)
         {
             switch(choice)
             {
                 case 1:
                 {
                     h=insertnode(h);
                     break;
                 }
                 case 2:
                 {
                     displaynodes(h);
                     clear_buffer();
                     getchar();
                     break;
                 }
                 default:
                         printf("Error: Invalid input\n");
                         clear_buffer();
                         getchar();
             }
             printf("\n1. Insert an element\n");
             printf("2. Display elements\n");
             printf("3. Exit \n");
        
             printf("\nEnter your choice\n");
             scanf("%d",&choice);
         }
    }
        
    /*myouput
    1. Insert an element
    2. Display elements
    3. Exit
    
    Enter your choice
    1
    Enter the name
    ?john
    
    1. Insert an element
    2. Display elements
    3. Exit
    
    Enter your choice
    1
    Enter the name
    ?alan
    
    1. Insert an element
    2. Display elements
    3. Exit
    
    Enter your choice
    1
    Enter the name
    ?brian
    
    1. Insert an element
    2. Display elements
    3. Exit
    
    Enter your choice
    2
    brian
    
    alan
    
    john
    
    */
    ssharish2005
    Last edited by ssharish2005; 10-17-2005 at 04:57 AM.

  3. #3
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335
    thanks. Just for reference the correct syntax is:

    strcpy(temp->customerName,customerName);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ Linked list program need help !!!
    By dcoll025 in forum C++ Programming
    Replies: 1
    Last Post: 04-20-2009, 10:03 AM
  2. how do i reverse a circular double linked list
    By kobra_swe in forum C Programming
    Replies: 13
    Last Post: 04-08-2008, 04:20 PM
  3. Replies: 11
    Last Post: 01-02-2006, 04:46 PM
  4. linked list question
    By yeahdixon in forum C++ Programming
    Replies: 2
    Last Post: 03-28-2002, 09:16 AM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM