简体   繁体   English

将指针取消引用到不完整类型的链表-C

[英]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 我尝试将struct声明更改为

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. 显然,您将struct声明放置在某个实现文件中,并且在该文件中放置了错误的实现文件。

The typedef declaration that you have in your header 标头中的typedef声明

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名称

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? 例如l可以等于NULL吗?

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? 您是否应该分配内存来存储参数名称指向的字符串的副本?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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