C错误 - 取消引用指向不完整类型的指针

问题描述 投票:-4回答:2

我试图从我创建的Tree字段获取信息,并且我在标题中收到错误:dereferencing指向不完整类型'struct List_t'的指针

树源文件:

    struct Node_t{
Element data;
char* location;
struct Node_t*  son;
struct Node_t* next; 
};
    struct List_t{
Node head;
copyFunc copyfunc;
compareFunc compfunc;
freeFunc freefunc;
printFunc printfunc;
};

树头文件:

typedef struct Node_t* Node;
typedef struct List_t* Tree;
typedef void* Element;

应用源文件:

Tree t;
t = createTree(compareInt, copyInt , freeInt, printInt);
int* x =(int*)malloc(sizeof(int));
*x=53;
Add(t, x);
char* location;
location= t->head->location; //here I got the error
printf(location);

我该怎么办?我究竟做错了什么?

谢谢!

c pointers dereference
2个回答
2
投票

struct List_t的声明需要在头文件中。随着createTree的声明。


0
投票

您提供了三段代码,并将其标识为:

  1. 树源文件:
  2. 树头文件:
  3. 应用源文件:

我们称这些文件为tree.ctree.happ.c

当您编译C源文件时,通常您有一个.c文件,其中可能包含以下行:

#include <stdio.h>
#include "tree.h"

在里面。这就是编译器知道如何去查看另一个文件中的定义。

如果你的app.c文件包含上面的行,那么app.c中的代码可能只使用stdio.htree.h提供的信息。

特别是,如果您在tree.c中提供了信息,那么app.c看不到该信息,因为没有引用它的#include指令。

解决方案(正如其他人所说)将你的struct定义和typedef语句以及公共接口的任何其他部分移动到tree.h文件中。

或者,如果您希望结构的成员是私有的,则可以提供返回数据的函数。当然,该函数的声明将是公共接口的一部分,因此它也必须在tree.h文件中(函数的定义可以在tree.c中,但声明将是公开的)。

© www.soinside.com 2019 - 2024. All rights reserved.