簡體   English   中英

編譯器錯誤-在C編程中取消引用不完整類型的指針

[英]compiler error - dereferencing pointer to incomplete type In C programming

您能為我解釋這個錯誤嗎?

在我的Ah文件中:

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

在我的Ac文件中:

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指針。 那么,為什么錯誤將指針取消引用為“不完整”類型呢?

因為在編譯main.c時,只有typdef是可見的,而不是struct Treenode的定義(在Ac中)。 因此,編譯器不知道結構中的內容,因此也不知道它甚至包含一個Root節點

TreeHead結構在Ac中定義,在Main.c中不可見

您必須將其放在頭文件中才能訪問它。

您還必須將結構放入頭文件中。

編譯器不知道main.c文件中結構的確切內存布局,因為它們沒有在頭文件中聲明。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM