Linked List of nodes with operators

This is a discussion on Linked List of nodes with operators within the C Programming forums, part of the General Programming Boards category; The info_node structure I made has hourly_wage components and job_title of 25 characters. I have built print_list, add_node. Still working ...

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    1

    Linked List of nodes with operators

    The info_node structure I made has hourly_wage components and job_title of 25 characters. I have built print_list, add_node. Still working on find and delete. But I want to make sure my print list and add node works first. But it wont compile because in my add_node function I get error:"incompatible types in assignment of 'char*' to char[25]. I know I shouldn't post programs that don't compile but I'm stuck and really need some assistance.
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #define SENT -1
    
    typedef struct info_node_s
    {
    char job_title[25];
    double hourly_wage;
    struct info_node_s *restp;
    } info_node_t;
    
    void print_list(info_node_t *n1_p);
    void add_node(info_node_t **n1_p, char job_title, double hourly_wage);
    /*void find_node(info_node_t **n1_p, char job_title);
    void delete_node(info_node_t **n1_p, char job_title);
    */
    main()
    {
          info_node_t *n1_p;
          char choice, A, R, F, D, Q;
          char job_title[25];
          double hourly_wage;
          n1_p = (info_node_t *)malloc(sizeof (info_node_t));
               printf("Enter A(dd), R(emove), F(ind), D(isplay), or Q(uit)>");
               scanf("  %c", &choice);
                                    if(choice==A){
                                            printf("Enter another job title \n");
                                            scanf(" %s", &job_title);
                                            printf("Enter another hourly wage \n");
                                            scanf(" %.2f", &hourly_wage);
                                            add_node(&n1_p, job_title, hourly_wage);
                                           }
                                   /*  else if(choice==R){
                                            printf("Enter job you want to delete \n");
                                            scanf("%s", &job_title);
                                            delete_node(n1_p,job_title);
                                            }
                                       else if(choice==F){
                                            printf("Enter job you want to find \n");
                                            scanf("%s", &job_title);
                                            find_node(n1_p, job_title);
                                            }*/
                                       else if(choice==D){
                                            print_list(n1_p);}
                                       else if(choice==Q){
                                            printf("Leaving the program\n");
                                            }
                                       else{
                                               printf("Illegal choice, try again\n");}
                                              System("pause");
                                               return (0);
    }
    
    void add_node(info_node_t **n1_p, char job_title[25],double hourly_wage)
    {
    
    info_node_t *temp;
    temp=*n1_p;
    
    if(*n1_p==NULL)
    {*n1_p=(info_node_t *)malloc(sizeof (info_node_t));
    temp=*n1_p;}
    else
    {while((temp->restp)!=NULL)
    {temp=temp->restp;}
    temp->restp=(info_node_t *)malloc(sizeof (info_node_t));
    temp=temp->restp;}
    temp->job_title=job_title;
    temp->hourly_wage=hourly_wage;
    temp->restp=NULL;
    }
    /*
    void delete_node(info_node_t **n1_p, char job_title)
    {
         
     }
    
    void find_node(info_node_t **n1_p, char job_title)
    {
     }
     */
    void print_list(info_node_t *n1_p)
    {
    if(n1_p==NULL)
    {printf("\n");}
    else {printf("%c , %.2lf ",n1_p->job_title,n1_p->hourly_wage);
    print_list(n1_p->restp);}
    }

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,498
    Posting programs that don't compile is fine, as long as you're asking about why they don't compile (which you are). The problem is you can't assign arrays (including char arrays used as strings) using the = sign. You have to use something like strcpy/strncpy to copy the contents (add string.h to your includes). There's a lot more errors and warnings you need to address. If your compiler didn't give you an equivalent list, turn the warning level up or get a better compiler. Here's what I got:

    $ gcc -Wall list.c
    list.c:18: warning: return type defaults to ‘int’
    list.c: In function ‘main’:
    list.c:28: warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘char (*)[25]’
    list.c:30: warning: unknown conversion type character ‘.’ in format
    list.c:30: warning: too many arguments for format
    list.c:31: warning: passing argument 2 of ‘add_node’ makes integer from pointer without a cast
    list.c:13: note: expected ‘char’ but argument is of type ‘char *’
    list.c:50: warning: implicit declaration of function ‘System’
    list.c:20: warning: unused variable ‘F’
    list.c:20: warning: unused variable ‘R’
    list.c: At top level:
    list.c:54: error: conflicting types for ‘add_node’
    list.c:13: note: previous declaration of ‘add_node’ was here
    list.c: In function ‘print_list’:
    list.c:86: warning: format ‘%c’ expects type ‘int’, but argument 2 has type ‘char *’

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Code:
    {while((temp->restp)!=NULL)
    {temp=temp->restp;}
    temp->restp=(info_node_t *)malloc(sizeof (info_node_t));
    temp=temp->restp;}
    temp->job_title=job_title;                        <--- You can't do this
    temp->hourly_wage=hourly_wage;
    temp->restp=NULL;
    C does not allow you to assign strings across the equals sign like that.
    Look in your C Library documentation for the strcpy() and strncpy() functions. They're what you need.

  4. #4
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,650
    Another one cross posting.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. deleting from a linked list if there are just 2 nodes
    By Nooby in forum C++ Programming
    Replies: 2
    Last Post: 04-10-2010, 11:55 PM
  2. Swap two nodes in linked list
    By shounakboss in forum C Programming
    Replies: 3
    Last Post: 09-12-2007, 10:18 AM
  3. Adding nodes to a linked list
    By bluescreen in forum C Programming
    Replies: 4
    Last Post: 11-09-2006, 12:59 AM
  4. interchange nodes in a linked list
    By Gustavo in forum C++ Programming
    Replies: 0
    Last Post: 10-26-2004, 07:14 PM
  5. Linked List and Nodes
    By paperbox005 in forum C++ Programming
    Replies: 2
    Last Post: 08-04-2004, 09:12 AM

Tags for this Thread


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