【问题标题】:compiler error - dereferencing pointer to incomplete type In C programming编译器错误 - 在 C 编程中取消引用指向不完整类型的指针
【发布时间】:2010-12-23 10:46:57
【问题描述】:

你能为我解释一下这个错误吗?

在我的 A.h 文件中:

struct TreeNode;
struct TreeHead;
typedef struct TreeNode * Node;
typedef struct TreeHead * Head;

在我的 A.c 文件中:

struct TreeNode {
    char* theData;
    Node Left;
    Node Right;
} ;

struct TreeHead{
    int counter;
    char type;
    Node Root;
};

Head Initialisation() {
    Head treeHead;
    treeHead = malloc(sizeof (struct TreeHead));
    treeHead->Root = malloc(sizeof (struct TreeNode));
    return treeHead;
}

在我的 Main.c 文件中:

Head head;
Node tree;
int choose =5;
head = Initialisation(); 
(head->Root) = tree; //When compiling, this line has an error: error: dereferencing pointer to incomplete type

haed->Root 会返回一个 Node 指针,tree 也是一个 Node 指针。那么为什么错误是取消引用指向“不完整”类型的指针呢?

【问题讨论】:

  • 你使用的是什么编译器。 gcc 可以使用您的代码语法。
  • 我使用 gcc 但我打开了所有警告和错误消息。

标签: c compiler-errors


【解决方案1】:

因为在编译 main.c 时,只有 typdef 是可见的,而不是 struct Treenode 的定义(在 A.c 中)。所以编译器不知道结构中有什么,所以甚至不知道它包含一个根节点

【讨论】:

  • 要对此进行扩展并“修复”您的问题:将结构定义移动到头文件:)
【解决方案2】:

TreeHead 结构体在 A.c 中定义,在 Main.c 中不可见

你必须把它放在一个头文件中才能访问它。

【讨论】:

    【解决方案3】:

    您还必须将结构体放入头文件中。

    编译器不知道 main.c 文件中结构的确切内存布局,因为它们没有在头文件中声明。

    【讨论】:

      猜你喜欢
      • 2018-10-23
      • 2011-02-04
      • 1970-01-01
      • 2014-09-06
      • 2017-02-13
      • 2018-05-31
      • 2017-11-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多