Thread: Error : incompatible types with structure

  1. #1
    Registered User
    Join Date
    Jul 2020
    Posts
    28

    Error : incompatible types with structure

    Hello,

    I am trying to build a data structure that consists of a single linked list (node_t).

    That for each entry in the list contains a key and a record structure (record_t) which is also a linked list.

    So conceptually:
    - Item 1
    - Record 1
    - Record 2
    - Item 2
    - Record 1
    - Record 2
    - Record 3

    and so on.

    The print_list function is supposed to read through all the records in the data structure and display them.

    Read through the items - while statement - commencing line 28

    Read through the records - while statement - commencing line 36

    but I get a build error on line 41, which is meant to advance to the next record (if it exists)

    Code:
    current->record = current->record.next;
    Code:
    |41|error: incompatible types when assigning to type ‘record_t’ from type ‘struct record *’|
    I don't understand what the compiler is trying to tell me here.

    Thanks

    VW
    Attached Files Attached Files

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,941
    The problem is that your struct node objects contain record_t objects rather than record_t pointers. This means that you don't have a pointer to the head of a linked list of records in each node; you have only one record per node.

    By the way, if appending to the linked list is a common operation, then you should represent the linked list as a struct containing both head and tail pointers. This way, you can append to the linked list in constant time rather than linear time.
    Last edited by laserlight; 07-16-2020 at 07:38 PM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jul 2020
    Posts
    28
    @laserlight

    Thanks for responding.

    Do you mean that the structure definition should be like this?

    Code:
        struct record {    char   c_detail1[20];
        int    i_value;
        };
    typedef struct record record_t;
    
    
        struct node {
        char   *key;
        record_t record;
        record_t *next;
        struct node *next;
    };
    typedef struct node node_t;
    I don't think this is correct as the IDE isn't auto-completing the elements of the structure correctly; but this is my interpretation of your reply.

    VW

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,941
    No, I would expect something like this:
    Code:
    struct record {
        char c_detail1[20];
        int i_value;
        struct record *next;
    };
    typedef struct record record_t;
    
    struct node {
        char *key;
        record_t *record_list;
        struct node *next;
    };
    typedef struct node node_t;
    In struct record, you need a next pointer because struct record represents a node in a linked list of records. In struct node, you need a pointer to struct record (i.e., record_t) as it contains a linked list of records represented by that struct record pointer to the head of the linked list. But you also need a next pointer because struct node itself represents a node in a linked list.

    Having said that, I misspoke slightly: if you really wanted, you could have a record_t object instead of a pointer as a member of struct node, except that it would be unusual since a pointer is more conventional. With a record_t object, you would be effectively saying that you have a list of records that always has at least one record, whereas with a pointer you could represent an empty linked list by a null pointer.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Jul 2020
    Posts
    28
    @laserlight

    Your advice regarding the structure has been very helpful.

    The "traditional" pointer approach makes more sense in my scenario.

    However I'm now encountering errors trying to refer to members of the record structure.

    Code:
    void print_list(node_t *node_list) {    node_t *current = node_list;
    
    
        printf("\n");
        while (current != NULL) {
                printf("Key: %s\n", current->key);
    
    
            while (current->record_list != NULL){
                printf("Detail: %s\n",current->record_list.c_detail1);
    //            printf("Value:  %d\n",current->record_list.i_value);
                printf("\n");
    
    
                current->record_list = current->record_list.next;
            }
    
    
            current = current->next;
        }
    }
    This line
    Code:
    printf("Detail: %s\n",current->record_list.c_detail1);
    Has error
    Code:
    error: request for member ‘c_detail1’ in something not a structure or union|
    Clearly there is something wrong here, but this still looks correct to me ??

    Can you see what the problem is, or advise a process that I could follow to figure it out myself?

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,176
    Please don't use @, it just makes work for us moderators as we have to approve your posts.
    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.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,941
    Quote Originally Posted by VeeDub
    Clearly there is something wrong here, but this still looks correct to me ??
    Now that you have record_list as a pointer, you need to access it as a pointer. But you wrote current->record_list.c_detail1, which accesses it as if it were a record_t object. One option is to write: current->record_list->c_detail1

    Note that you should be using a temporary record_t pointer to iterate over the linked list of records. The way you're doing it now loses the pointer to the head of the linked list of records.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 06-01-2015, 07:13 AM
  2. incompatible types error
    By solaris252 in forum C Programming
    Replies: 4
    Last Post: 11-23-2013, 01:21 AM
  3. Getting "incompatible types in assignment error."
    By mgracecar in forum C Programming
    Replies: 1
    Last Post: 02-29-2012, 06:38 PM
  4. Replies: 2
    Last Post: 12-26-2009, 10:07 AM
  5. Incompatible types in assignment error
    By Zildjian in forum C Programming
    Replies: 12
    Last Post: 10-03-2003, 01:15 PM

Tags for this Thread