2014-09-20 133 views
-1

我學習如何在C中創建鏈接列表。請看this article數據類型* <變量名稱>與數據類型* <變量名稱>之間的區別

首先,他使用以下代碼創建結構;

struct node 
{ 
    int data; 
    struct node *next; 
}; 

其明確說明* next是類型節點的指針變量。

但是當他前進時,他這樣做;

struct node* head = NULL; 
struct node* second = NULL; 
struct node* third = NULL; 

現在,在這裏我有一個問題,理解他正在嘗試做什麼;他是在創建名稱,頭部,第二和第三個節點嗎?或者他只是試圖創建類型節點的指針變量?

因爲他把它們等於NULL;我假設他正在嘗試創建指針變量。但是他不能用這個做同樣的事嗎?

struct node *head = NULL; 
struct node *second = NULL; 
struct node *third = NULL; 

由於

+0

那麼'struct node * head'和struct node'struct node * head'之間的區別呢? – 2014-09-20 21:56:24

+1

'a = a + b'和'a = a + b'有什麼區別? – dari 2014-09-20 21:56:28

+0

'struct node * head'和'struct node * head'和'struct node * head'和'struct node * head'沒有區別。 – 2014-09-20 21:56:37

回答

4

在C中,之前或之後的*是無意義的空格。所以:

struct node *head; 
struct node * head; 
struct node* head; 
struct node*head; 

都完全一樣。 C不關心那個空白。

當你遇到麻煩是,當你聲明多個項目:

struct node *head, tail; // tail is not a pointer! 
struct node *head, *tail; // both are pointers now 
struct node * head, * tail; // both are still pointers; whitespace doesn't matter 
1

兩者在技術上是相同.....

struct node *third = NULL; 
struct node* third = NULL; 

做同樣的事情,因爲編譯器不數空白。

相關問題