2015-07-01 105 views
5

我不知道,什麼是之間的區別:函數參數中的struct關鍵字有什麼區別?

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

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

爲什麼我們需要在第二個例子中struct關鍵字?

另外,就是

void Foo(Node* head) 
{ 
    Node* cur = head; 
    //.... 
} 

void Foo(struct Node* head) 
{ 
    struct Node* cur = head; 
    //.... 
} 
+1

這應該回答你的問題:http://stackoverflow.com/questions/8422775/why-does-c-need-struct-keyword-and-not-c – matb

+2

C *或* C++?答案根據語言完全不同。 – Quentin

+0

昆汀說什麼。刪除C++或C標記。 –

回答

4

只有聲明,包括struct是有效的C.有一個在C++中沒有差異的區別。

但是,你可以在typedefstruct C中,所以你不必每次寫它。

typedef struct Node 
{ 
    int data; 
    struct Node *next; // we have not finished the typedef yet 
} SNode; 

SNode* cur = head; // OK to refer the typedef here 

此語法在C++中也是有效的,以便兼容。

+3

*「在C++中沒有什麼區別。」* - 這不是真的......使用'struct Node * next;'只是以該名稱搜索'struct' /'class' /'union'並且愉快地忽略具有相同標識符的非''struct' /'class' /'union's。例如,如果向'struct Node;'添加一個'int Node;'數據成員,它將不會與'struct Node * next;'發生衝突,但會與'Node * next;'發生衝突。沒有人理智依賴於這種區別 - 使得不可維護的代碼。 –

0

Struct節點是我們創建的新的用戶定義的數據類型。與類不同,使用結構的新數據類型是「struct strct_name」,即;你需要struct_name前面的關鍵字struct。 對於類,在新的數據類型名稱前面不需要關鍵字。 例如;

class abc 
{ 
    abc *next; 
}; 

和當u的結構的情況下,聲明變量

abc x; 

不是struct

abc x; 

。 還了解到,該語句

struct node * next; 

我們正在嘗試,因爲它指向父結構來創建一個指向型「strcut節點」的一個變量,它被稱爲在這種情況下,自我引用指針的指針。

相關問題