2013-02-24 69 views
0

我正嘗試爲二叉樹創建一個新節點,並且在嘗試爲 - >左側和右側進行分配時出現錯誤。二進制樹節點 - GCC警告 - 不兼容類型

typedef struct bin_node_t { 
    data_t data; 
    bst_key_t key; 
    struct bin_node *left; 
    struct bin_node *right; 
} bin_node; 

是我的結構定義bin_node。

下面是我使用的變量:

bin_node *new; 
bin_node *node_array[256]; 

這裏是我的變量賦值:

/* ... code to initialize node_array ... */ 
new = (bin_node *)malloc(sizeof(bin_node)); 

這裏的地方我剛開了一個錯誤:

new->right = node_array[i+1]; 
    new->left = node_array[i]; 

和這裏是我得到的編譯器警告:

huffman.c:99: warning: assignment from incompatible pointer type 
huffman.c:100: warning: assignment from incompatible pointer type 

我完整的代碼,請訪問:

http://pastebin.com/iPYP5uVt

+1

難道是您忘記了左指針聲明和右指針聲明中的_t? – Gene 2013-02-24 22:33:16

+1

如果您打算使用'_t'後綴,則在typedef名稱中使用它比在結構標記中使用它更正常(或者,當您在結構標記中使用它時,還可以在typedef名稱中使用它)。因此'typedef struct bin_node {...} bin_node;'或'typedef struct bin_node_t {...} bin_node_t;'會更傳統。請注意,[POSIX保留typedef名稱的_t後綴](http://stackoverflow.com/a/231807/15168);你在名義上使用它在薄冰上行走,但許多人都樂於忽視這一點,大多數時候他們沒有任何麻煩就逃脫了。 – 2013-02-24 22:53:37

+0

最佳做法是在應用程序代碼中根本不使用'_t',因爲此約定是爲C庫類型保留的。 – Gene 2013-02-25 00:59:28

回答

5

有沒有在你的代碼中沒有struct bin_node。將struct bin_node *left更改爲:

struct bin_node_t *left; 
       ^^^ 
+0

是的,我很笨,謝謝! – 2013-02-25 01:39:50