typedef struct 和 struct區別

來源:互聯網
上載者:User

(1) struct{ int x; int y; }test1; 好,定義了 結構 test1,test1.x 和 test1.y 可以在語句裡用了。(2) struct test {int x; int y; }test1; 好,定義了 結構 test1,test1.x 和 test1.y 可以在語句裡用了。與 1 比,省寫 了 test(3) typedef struct test {int x; int y;  // 你漏打分號,給你添上 }text1,text2; 只說了 這種結構 的(類型)別名 叫 text1 或叫 text2真正在語句裡用,還要寫:text1 test1;然後好用 test1.x test1.y或寫 text2 test1;然後好用 test1.x test1.y(4)type struct {int x; int y; }test1;這個不可以。改 typedef ... 就可以了。但也同 (3)一樣,還要 寫:test1 my_st;才能用 my_st.x 和 my_st.y

 

 

struct{成員表列}變數名表列1,變數名表列2;(變數名表列1,變數名表列2)這2個變數名怎麼理解? 一個是聲明一個結構體的原始用法,一個是把結構體取了個別名。例如struct list{  int a;  char c;  stuct list *next;};(注意這個分號不能少)這個結構體的類型名是struct list ;struct只是聲明結構體需要的關鍵字。 如果你要定義一個結構體變數 你就可以 :結構體類型 變數名如:struct list student;你是不是覺得寫struct list 覺得很麻煩?那你就可以把struct list起個別名;就是綽號可以這樣struct list{  int a;  char c;  stuct list *next;};typedef struct list A;那麼你的A student; 就等同與 struct list student;。當然你也可以在聲明一個結構體的時候給它起別名typedef struct list{  int a;char c;struct list *next;}A;

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.