Thread: assignment from incompatable pointer

  1. #1
    Registered User
    Join Date
    Apr 2019
    Posts
    662

    assignment from incompatable pointer

    i have this code
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    typedef struct Node
    {
        int data;
        struct node *next, *previous;
    }Node;
    
    typedef struct LinkedList
    {
        struct Node *head, *tail;
    }LinkedList;
    
    void createhead(LinkedList *list);
    int getdata(void);
    Node *getmemory(void);
    void appendlist(LinkedList *list);
    void printlist(const LinkedList list);
    void deletelist(LinkedList *list);
    
    int main()
    {
        LinkedList list;
    
        createhead(&list);
        appendlist(&list);
        appendlist(&list);
        appendlist(&list);
        printlist(list);
        deletelist(&list);
    
        return 0;
    }
    
    void createhead(LinkedList *list)
    {
        Node *tmpnode = NULL;
    
        list->head = NULL;
        list->tail = NULL;
    
        tmpnode = getmemory();
        if (!tmpnode)
        {
            printf("error getting memory");
            exit(EXIT_FAILURE);
        }
    
        tmpnode->data = getdata();
        tmpnode->previous = NULL;
        tmpnode->next = NULL;
        list->head = tmpnode;
        list->tail = list->head;
    }
    
    int getdata(void)
    {
        int data;
    
        printf("Please enter a number: ");
        scanf(" %d", &data);
    
        return data;
    }
    
    Node *getmemory(void)
    {
        return malloc(sizeof(Node));
    }
    
    void appendlist(LinkedList *list)
    {
        Node *tmpnode = NULL;
    
        tmpnode = getmemory();
    
        if (!tmpnode)
        {
            printf("error getting memory");
            return;
        }
    
        tmpnode->data = getdata();
        tmpnode->next = NULL;
        list->tail->next = tmpnode;
        list->tail = tmpnode;
        if (!list->head->next)
        {
            list->head->next = tmpnode;
        }
    }
    
    void printlist(const LinkedList list)
    {
        int count = 1;
        Node *current = list.head;
    
        while (!current)
        {
            printf("node %d's data is %d", count, current->data);
            count++;
            current = current->next;
        }
    }
    
    void deletelist(LinkedList *list)
    {
        int count = 1;
        Node *tmpnode = NULL;
    
        while (list->head)
        {
            tmpnode = list->head->next;
            free(list->head);
            printf("node %d freed", count);
            count++;
            list->head = tmpnode;
        }
    }
    lines 86 90 103 and 114 i get the warning yet i have done no different than what i have done previously here post 1 line 146 linked list sorting function causes segfault
    has the wind changed direction again without me noticing??

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,853
    Remember to carefully read through the entire error message: it is telling you what's wrong. Look carefully at the line number too (from the line numbering given by this forum software, I can see a mistake on line 87, not 86).

    EDIT:
    Oh, actually, you may be right to say that there's an error on line 86. Not that the mistake is on line 86; the mistake is on line 7. Check your spelling.
    Last edited by laserlight; 05-31-2019 at 02:37 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
    Apr 2019
    Posts
    662
    opps that's the issue with sort names its easier to type the whole thing rather than select the correct auto complete. all works now. However i fail to see the mistake on line 87 and gcc doesn't complain.

    next silly question is why didn't i get an error for line 7 when i did elsewhere for the same mistake;

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,853
    Oh, looks like I was thinking of something else on line 87.

    I suggest that you post the entire error message in the future.
    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
    Apr 2019
    Posts
    662
    i changed Node back to node to recreate the warnings
    ||=== Build: Debug in double linked lists (compiler: GNU GCC Compiler) ===|
    /home/ben/Documents/coding/double linked lists/main.c||In function ‘appendlist’:|
    /home/ben/Documents/coding/double linked lists/main.c|93|warning: assignment from incompatible pointer type [-Wincompatible-pointer-types]|
    /home/ben/Documents/coding/double linked lists/main.c|94|warning: assignment from incompatible pointer type [-Wincompatible-pointer-types]|
    /home/ben/Documents/coding/double linked lists/main.c|98|warning: assignment from incompatible pointer type [-Wincompatible-pointer-types]|
    /home/ben/Documents/coding/double linked lists/main.c||In function ‘printlist’:|
    /home/ben/Documents/coding/double linked lists/main.c|111|warning: assignment from incompatible pointer type [-Wincompatible-pointer-types]|
    /home/ben/Documents/coding/double linked lists/main.c||In function ‘deletelist’:|
    /home/ben/Documents/coding/double linked lists/main.c|122|warning: assignment from incompatible pointer type [-Wincompatible-pointer-types]|
    /home/ben/Documents/coding/double linked lists/main.c||In function ‘prependlist’:|
    /home/ben/Documents/coding/double linked lists/main.c|141|warning: assignment from incompatible pointer type [-Wincompatible-pointer-types]|
    /home/ben/Documents/coding/double linked lists/main.c|142|warning: assignment from incompatible pointer type [-Wincompatible-pointer-types]|
    /home/ben/Documents/coding/double linked lists/main.c||In function ‘reverseprintlist’:|
    /home/ben/Documents/coding/double linked lists/main.c|156|warning: assignment from incompatible pointer type [-Wincompatible-pointer-types]|
    /home/ben/Documents/coding/double linked lists/main.c||In function ‘countnodes’:|
    /home/ben/Documents/coding/double linked lists/main.c|168|warning: assignment from incompatible pointer type [-Wincompatible-pointer-types]|
    ||=== Build finished: 0 error(s), 9 warning(s) (0 minute(s), 0 second(s)) ===|

    so other than the os i am using and the project path and directory what did i miss out
    coop

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. incompatable type for argument 1 of enter address
    By cooper1200 in forum C Programming
    Replies: 2
    Last Post: 05-18-2019, 04:09 AM
  2. Replies: 21
    Last Post: 11-25-2014, 12:30 PM
  3. Pointer Assignment
    By cardinals03 in forum C++ Programming
    Replies: 2
    Last Post: 10-22-2009, 12:16 PM
  4. Replies: 1
    Last Post: 03-24-2008, 10:16 AM
  5. Assignment [pointer]
    By aotomato in forum C++ Programming
    Replies: 4
    Last Post: 11-22-2004, 04:31 AM

Tags for this Thread