Thread: dereferencing pointer to incomplete type

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    6

    dereferencing pointer to incomplete type

    Hello, I am making an application that implements doubly linked lists (each node has a pointer to the previous node and the next node), and I am am having trouble with the functions that service the list. I am getting a compiler error "dereferencing pointer to incomplete type", and I am unsure of how to proceed.

    Here is the relevant code

    In list.h

    Code:
     /* linked list node */
    typedef struct _lnode {
    
      struct lnode *prev;
    
      struct lnode *next;
    
      int data;
    
    } lnode;
    
    /* struct for list, contains a pointer to the head of the list */
    typedef struct _llist {
    
      lnode *head;
    
    } llist;
    in list.c
    Code:
    /* adds a node to the front of the list given a pointer to the list and the node to add
    * replaces current head.
    */
    
    void add_front(llist* list, lnode* node) {
      /* new head needs to point to old head as its next.
       * new head needs to point to old tail as its prev.
       * old head needs to point to new head as its prev.
       * tail needs to point to new head as its next.
       * list needs to recognize new head as head.
       */
      
      node->next = (struct lnode *) list->head; 
      node->prev = list->head->prev;
      //ERROR OCCURS NEXT LINE
      list->head->prev->next = node;
      list->head->prev = (struct lnode *)node;
      list->head = node;
     
    }
    list.h is included, so the compiler can find everything okay, so I don't know what 'incomplete type'
    is referring to. On a side note, if I change
    Code:
    typedef struct _llist {
    
      lnode *head;
    
    } llist;
    to
    Code:
    typedef struct _llist {
    
      struct lnode *head;
    
    } llist;
    I will get a lot of the same error on different statements in the add_front function. I am not sure why this happens either. Is there something fundamental about pointers/structs that I am not getting? Thanks in advance.
    Last edited by kataya; 04-16-2008 at 01:29 AM.

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Code:
    typedef struct _lnode {
    
      struct lnode *prev;
    You are doing wrong because you cannot use a name that technically hasn't been defined yet. Instead, use the actual structure name:

    Code:
    typedef struct _lnode {
    
      struct _lnode *prev;
    Also note that since you are using typedef, you should not be typing "struct" before it, because lnode is a typedef for "struct _lnode." That means "struct lnode" becomes "struct struct _lnode," which you can see is wrong.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    6
    Ah nice, it compiles now. Thanks much for the fix and more so for the explanation.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Replies: 5
    Last Post: 04-04-2009, 03:45 AM
  3. failure to import external C libraries in C++ project
    By nocturna_gr in forum C++ Programming
    Replies: 3
    Last Post: 12-02-2007, 03:49 PM
  4. dereferencing void pointer
    By nkhambal in forum C Programming
    Replies: 4
    Last Post: 04-25-2005, 02:47 AM
  5. Learning OpenGL
    By HQSneaker in forum C++ Programming
    Replies: 7
    Last Post: 08-06-2004, 08:57 AM