如果我創建了一個結構:結構和類型定義使用C
struct student {
int id;
char name [20];
student * next};
是什麼意思:
typedef struct student student?
什麼student * next
?
謝謝!
如果我創建了一個結構:結構和類型定義使用C
struct student {
int id;
char name [20];
student * next};
是什麼意思:
typedef struct student student?
什麼student * next
?
謝謝!
A typedef
創建一個新名稱。因此,這些是完全等效的:在你的例子中,struct和typedef具有相同的名稱。但它可能是這樣的:
struct something {
/* ... */
};
typedef struct something student;
typedef的聲明看起來像它引入的類型。
作爲一面注,things are different in C++。
在C中,無論何時使用結構,都必須使用關鍵字struct
。通過輸入typedef struct student student
,你可以簡單地說說student
。
使用命名結構,關鍵字struct
通常用於創建結構實例並引用結構類型。
的typedef
讓你幾乎創建一個別名爲student
,而不必使用struct student
不斷,從而真正使得它更像一個內置的類型。
typedef struct student student;
這創造struct student
別名名稱爲student
。
結構標籤和類型名稱存在於C中的兩個不同名稱空間中(C++中不是這樣),所以允許爲別名和標籤使用相同的名稱。
你有沒有考慮閱讀[介紹性書](http://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list)? – 2012-02-07 14:21:27