简体   繁体   中英

Dereferencing Pointer to incomplete type Linked List - C

I've been trying to figure this out for a while now, but cannot find a solution. I am building a linked list and when I try to pass the list as a pointer to anything I get an error: Dereferencing Pointer to incomplete type.

Here is my struct declaration

typedef struct listStruct{
 char *name;
 int size;
 boolean inRestStatus;
 list *next;
 }list;

and one of the many functions that do not work.

void addToList(list *l, char * name, int size){
list *tmp;
while(l->next != NULL){
        l = l->next;
        }
tmp = malloc(sizeof(list));
tmp->name = name;
tmp->size = size;
tmp->inRestStatus = NO;
tmp->next = NULL;
l->next = tmp;
}

and the header

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct listStruct list;

I have tried changing the struct declaration to

typedef struct listStruct list{
...
};

and received the error: request for member in something not structure or union. If anyone has any ideas that'd be awesome.

Edit

The struct definition is/was in a main function in a seperate file than the function, I have since moved the definition to the header file.

Apparently, you placed your struct declaration into some implementation file, and a wrong implementation file at that.

The typedef declaration that you have in your header

typedef struct listStruct list;

declares an incomplete type. You have to place this

typedef struct listStruct{
  char *name;
  int size;
  boolean inRestStatus;
  list *next;
} list;

into the header or at least into the same implementation file that uses the data fields of your struct. Where is it now? You have to describe your file structure in full detail.

It seems that you declared only typedef name in the header

typedef struct listStruct list;

Thus the module where function

void addToList(list *l, char * name, int size);

is defined does not know the definition of the structure.

You have to include the structure definition in the header as for example

typedef struct listStruct{
 char *name;
 int size;
 boolean inRestStatus;
 struct listStruct *next;
 }list;

that it would be accessible in the module where the function is defined.

Take into account that this method

void addToList(list *l, char * name, int size){
list *tmp;
while(l->next != NULL){
        l = l->next;
        }
tmp = malloc(sizeof(list));
tmp->name = name;
tmp->size = size;
tmp->inRestStatus = NO;
tmp->next = NULL;
l->next = tmp;
}

is also wrong. For example l can be equal to NULL can't it?

Also simple copying pointers

tmp->name = name;

looks questionably. Should you allocate memory to store a copy of a string pointed to by argument name?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM