2012-09-13 50 views
1

我一直在試圖讓一個指針等於另一個指針相當一段時間,但它只是不會這樣做,我不知道爲什麼。內部結構中使用指針相等

的結構是:

typedef struct{ 
    struct listNode* next; 
} listNode; 

typedef struct{ 
    listNode* head; 
} linkedList; 

但是在代碼中,當我嘗試執行: 節點 - >未來=列表 - >頭

我收到了「在不兼容的指針類型分配」錯誤。 任何幫助將不勝感激,因爲我真的可以看到任何錯誤! 謝謝

回答

1

你在上面typedef提到struct listNode,但沒有聲明。

你需要做的事情以正確的順序:

   +----At this point, we can refer to "struct listNode" 
       v 
struct listNode { 
    struct listNode *next; /* We know this self-references, uses the same name. */ 
}; 

/* Now establish a shorthand name for the struct ... */ 
typedef struct listNode listNode; 

/* ... which we can use in subsequent declarations like this. */ 
typedef struct { 
    listNode *head; 
} linkedList;