Thread: "dereferencing pointer to incomplete type"

  1. #1
    anguished incognito54's Avatar
    Join Date
    Apr 2004

    "dereferencing pointer to incomplete type"

    Hi there!

    So I'm doing this assignment where I have to make a simple thread library. I have a double linked list of tthreads but when accessing them I get a "dereferencing pointer to incomplete type" error.

    The sthread_user.c file which contains the actual definition of the thread looks like this:

    #include <stdlib.h>
    #include <assert.h>
    #include <stdio.h>
    #include <sthread.h>
    #include <sthread_user.h>
    #include <sthread_ctx.h>
    #include <sthread_preempt.h>
    #include "dlinkedlist.h"
    struct _sthread {
      int tid;
      int created;
      int deathflag;
      sthread_ctx_t *saved_ctx;
      sthread_ctx_t *return_ctx;
      sthread_start_func_t func;
      void *args;
    int id = 0;
    list *thread_list = NULL;
    /* Part 1: Creating and Scheduling Threads                           */
    void dispatcher() {
      struct _sthread *temp = thread_list->next_runnable;
      sthread_ctx_t *larilas = sthread_new_blank_ctx();
      sthread_switch(larilas, temp->return_ctx);
    void sthread_user_init(void) {
      struct _sthread *thread;
      if(thread_list == NULL)
        thread_list = createlst();
      thread = malloc(sizeof(struct _sthread));
      thread->tid = ++id;
      thread->created = 0;
      thread->deathflag = 0;
      thread->saved_ctx = sthread_new_blank_ctx();
      thread->return_ctx = sthread_new_blank_ctx();
      thread->args = NULL;
      insertlst(thread_list, thread);
    sthread_t sthread_user_create(sthread_start_func_t start_routine, void *arg) {
      struct _sthread *temp =  thread_list->next_init;
      temp->args = arg;
      temp->func = start_routine;
      temp->saved_ctx = sthread_new_ctx((sthread_ctx_start_func_t) dispatcher);
      thread_list->next_runnable = temp;
      return (sthread_t)temp;
    void sthread_user_exit(void *ret) {
    void sthread_user_yield(void) {
      struct _sthread *temp = thread_list->next_runnable;
      temp->return_ctx = sthread_new_blank_ctx();
      sthread_switch(temp->return_ctx, temp->saved_ctx);
    and the sthread_user.h like this

    #ifndef STHREAD_USER_H
    #define STHREAD_USER_H 1
    /* Part 1: Basic Threads */
    void sthread_user_init(void);
    sthread_t sthread_user_create(sthread_start_func_t start_routine, void *arg);
    void sthread_user_exit(void *ret);
    void sthread_user_yield(void);
    /* Part 2: Synchronization Primitives */
    sthread_mutex_t sthread_user_mutex_init(void);
    void sthread_user_mutex_free(sthread_mutex_t lock);
    void sthread_user_mutex_lock(sthread_mutex_t lock);
    void sthread_user_mutex_unlock(sthread_mutex_t lock);
    sthread_sem_t sthread_user_sem_init(int value);
    void sthread_user_sem_free(sthread_sem_t sem);
    void sthread_user_sem_wait(sthread_sem_t sem);
    void sthread_user_sem_post(sthread_sem_t sem);
    #endif /* STHREAD_USER_H */
    I know that typedef struct should be in the .h file but the assignemt clearly says we can't touch that file and the only file we can edit is the sthread_user.c.

    My list code file and header files are:

    #include <stdlib.h>
    #include <stdio.h>
    #include <sthread.h>
    #include <sthread_user.h>
    #include <sthread_ctx.h>
    typedef struct _node{
      struct _node *prev;
      struct _node *next;
      struct _sthread *thread;
    } node;
    typedef struct _list{
      struct _node *first;
      struct _node *last;
      struct _sthread *next_init;
      struct _sthread *next_runnable;
    } list;
    list *createlst();
    void insertlst(list *list, struct _sthread *thread);
    void removelst(list *list, int tid);
    struct _sthread *getthread(list *list, int tid);
    void get_next_init(list *list);
    and the code file
    #include "dlinkedlist.h"
    list *createlst() {
      list *new = malloc(sizeof(list));
      new->first = NULL;
      new->last = NULL;
      new->next_init = NULL;
      new->next_runnable = NULL;
      return new;
    void insertlst(list *list, struct _sthread *thread) {
      node *new = malloc(sizeof(node));
      new->thread = thread;
      new->prev = list->last;
      if(list->first == NULL) {
        list->first = new;
        list->next_init = new->thread;
      else {
        list->last->next = new;
      list->last = new;
    void get_next_init(list *list) {
      node *temp;
      for(temp = list->first; temp != NULL; temp = temp->next) {
        if(! temp->next->thread->created ) /*the line where i get the error*/
          list->next_init = temp->thread;
    and i get that error in the if(! temp->next->thread->created ) line but I can't figure out why. I tried renaming and putting (()) and *'s and it just doesn't work...

    I've never felt the nausea of longing to feel nothing,
    I never wanted to cease to exist, just disappear...

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Northern Virginia/Washington DC Metropolitan Area
    When your list.c file gets compiled, the compiler must know what an _sthread is (as it is part of a _node struct) in order to generate object code from that source file. Since the declaration for the _sthread struct appears to only be in the sthread_user.c source file it cannot do so. The declaration of the _sthread struct MUST be visible within the list.c file through some means in order for you to successfully compile.

    ...are you sure that the only file you can edit is the sthread_user.c file and not any of the others?
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    anguished incognito54's Avatar
    Join Date
    Apr 2004
    the list files are mine, those i can edit.

    I tried that in the meantime and it works, but it just doesn't feel right... I did a search here in the forum and I found a solution with extern but it doesn't work for me...
    I've never felt the nausea of longing to feel nothing,
    I never wanted to cease to exist, just disappear...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems passing a file pointer to functions
    By smitchell in forum C Programming
    Replies: 4
    Last Post: 09-30-2008, 02:29 PM
  2. Ban pointers or references on classes?
    By Elysia in forum C++ Programming
    Replies: 89
    Last Post: 10-30-2007, 03:20 AM
  3. Question About Pointer To Pointer
    By BlitzPackage in forum C++ Programming
    Replies: 2
    Last Post: 09-19-2005, 10:19 PM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM