Thread: linked list - problem

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    58

    Question linked list - problem

    Hi,
    could you please help me with my cose.
    I don't understand what is wrongvwith the:
    "append (head, read_data());"

    the error:
    incompatible type for argument 2 of ‘append’


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    enum status{marrid=1,devorse,single};
    enum department{RandD=1,marketing,testing};
    typedef struct employee
    {
        char f_name[10];
        char l_name[10];
        int ID ;
        enum status Fstatus;
        enum department Wdepartment;
        int Age;
        int Sallery;
        int Seniority;
    }employee;
    
     struct node
    {
         employee emp;
        struct node* next;
    };
    
    typedef employee* EmployeePtr;
    const MAX = 3;
    int read_data()
    
    {
        EmployeePtr p;
        printf("Enter your first name:\n ");
        //flushall();
        gets(p->f_name);
        printf("Enter your last name:\n ");
        //flushall();
        gets(p->l_name);
        printf("Enter  ID:\n ");
        //flushall();
        scanf ("%d",&p->ID);
        printf("Enter  Fstatus(marrid=1,devorse=2,single=3):\n ");
        //flushall();
        scanf ("%d",&p->Fstatus);
        printf("Enter  Wdepartment(RandD=1,marketing=2,testing=3):\n ");
        //flushall();
        scanf ("%d",&p->Wdepartment);
        printf("Enter  Age: \n");
        //flushall();
        scanf ("%d",&p->Age);
        printf("Enter  Sallery:\n ");
        //flushall();
        scanf ("%d",&p->Sallery);
        printf("Enter  Seniority: \n");
        //flushall();
        scanf ("%d",&p->Seniority);
        return p;
    }
    
    void append (struct node* head, employee emp) // add
    {
        struct node* new_last;
        struct node* n_last = new_last;// node; //n_last = new last
        static struct node* last;
        n_last->emp = emp;
        n_last->next = NULL;
    
        if (head == NULL)
        {
            head = n_last;
        }
        else
        {
            last->next = n_last;
             last = n_last;
        }
    }
    
    void bulid_list (struct node* head)
    {
        int i;
        for (i = 0; i < MAX; i++)
        {
            append (head, read_data());
        }
    }
    
    
    
    
    void read_2_file (struct node* head, FILE *f)
    {
        f = fopen ("employee.txt", "wt");
        struct node* temp = head;
    
        while (temp != NULL)
        {
            fprintf (f, "%s %s %8.2f\n", temp->emp.f_name, temp->emp.l_name,temp->emp.ID);
            temp = temp->next;
        }
        fclose(f);
    }
    
    int main()
    {
    
    
    
        return 0;
    
    }

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    You should be getting a lot more errors than that:
    Code:
    $ make list
    gcc -g -Wall  -o list list.c -lpthread -lm -lpq
    list.c:26:7: warning: type defaults to ‘int’ in declaration of ‘MAX’ [-Wimplicit-int]
    list.c: In function ‘read_data’:
    list.c:42:5: warning: format ‘%d’ expects argument of type ‘int *’, but argument 2 has type ‘enum status *’ [-Wformat]
    list.c:45:5: warning: format ‘%d’ expects argument of type ‘int *’, but argument 2 has type ‘enum department *’ [-Wformat]
    list.c:55:5: warning: return makes integer from pointer without a cast [enabled by default]
    list.c: In function ‘bulid_list’:
    list.c:82:9: error: incompatible type for argument 2 of ‘append’
    list.c:58:6: note: expected ‘employee’ but argument is of type ‘int’
    list.c: In function ‘read_2_file’:
    list.c:96:9: warning: format ‘%f’ expects argument of type ‘double’, but argument 5 has type ‘int’ [-Wformat]
    make: *** 
    [list] Error 1
    Line 26: const int MAX = 3; Don't let the compiler assume anything, you need that int there.
    Lines 42, 45: An enum is not guaranteed to be an int, thus an enum * and a int * are not compatible types. Read into an int variable, check it is a valid enum value, then assign to Wdepartment.
    Lines 55, 58, 82: see below
    Line 96: You need a %d if you want to print out an int.


    As for the error you mentioned:
    Code:
    void append (struct node* head, employee emp)
    append takes an employee type as the second argument.
    Code:
    append (head, read_data());
    You pass it the return value of read_data.
    Code:
    int read_data()
    read_data returns an int, hence the error. An employee type is not an int.

    But wait, there's more:
    Code:
    int read_data()
    {
        EmployeePtr p;
    ...
        return p;
    You told the compiler you would return an int from read_data, but you are returning p, which is not an int, it is a type EmployeePtr.

    Not to be rude, but this is a mess. It's not really your fault though. It looks like you found the code (or at least part of it) here: * îääúçìä C *. My browser didn't deal with the right-to-left text very well, and I don't speak a lick of Hebrew, but I did notice a few red flags. First, this tutorial is using #include <conio.h>. That is a very old DOS header, that is only used by crappy, outdated compilers (Turbo C). Second, the author uses the gets() function. gets is not recommended. In fact, it's so bad, my compiler complains when I try to use it. Read this: FAQ > Why gets() is bad / Buffer Overflows - Cprogramming.com.

  3. #3
    Registered User
    Join Date
    Feb 2013
    Posts
    58
    change the read_data() function to void ,but the same error ...

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Quote Originally Posted by ilans11il View Post
    change the read_data() function to void ,but the same error ...
    Quote Originally Posted by anduril462 View Post
    but you are returning p, which is not an int, it is a type EmployeePtr.
    It's like you never read the post at all, and just made some random change and replied "nope, still doesn't work". There was no thought in the change, just press a few random keys and try it again.

    As anduril462 said, you need to stop being a copy/paste merchant and start writing some code for yourself.

    It's going to be tough to break the habit of find some code, get some fixes on one board, then get some more fixes on another board
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Feb 2013
    Posts
    58
    1- I read the ALL post
    2 - I'm tryng to implement my code on a exiating code
    3 - I realy don't know how to solve the problem since I'm new on linked list

  6. #6
    Registered User
    Join Date
    Feb 2013
    Posts
    58
    another thing:
    I you don't want to replay/share your knowledge ,than just don't !!!
    you don't need to replay to all my threads

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    I'm only interested in sharing my knowledge with people who seem to be putting in an honest effort to LEARN.

    When evidence to the contrary is found (cross-posting, homework by approximation, exceedingly sloppy work, regurgitated code from somewhere else), then I (and most everyone else) simply lose interest in helping. It's simply NOT worth dumping "knowledge" on you, if all you're going to do with it is run to teacher with your apparent "look what I did (gimme good grades)".

    Either you learn this now, or you learn it in a few years when you've got a much more expensive college debt to pay off and you can't hold onto a job for more than a few weeks before people employing you realise you can't program your way out of a wet paper bag.

    So, buckle up, start doing your OWN work, or find another career choice.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with linked list
    By Zeevi88 in forum C Programming
    Replies: 6
    Last Post: 02-04-2012, 03:42 AM
  2. problem with linked list
    By nickman in forum C Programming
    Replies: 12
    Last Post: 06-09-2010, 10:06 AM
  3. Replies: 6
    Last Post: 03-02-2005, 02:45 AM
  4. linked list problem
    By cathzee in forum C Programming
    Replies: 1
    Last Post: 11-07-2003, 08:11 AM
  5. syntax linked list problem & struct problem
    By beely in forum C Programming
    Replies: 5
    Last Post: 11-11-2002, 09:14 AM