[C語言 - 9] typedef

來源:互聯網
上載者:User

標籤:

關鍵字typedef

  typedef 操作符可以看做是普通變數和類型之間的轉換開關!!例如typedef int Integer;//定義了一種類型int Integer;//定義了一個變數就是類型和變數的區別 A. 對資料類型定義“別名"
1 int main(int argc, const char * argv[]) {2     typedef int integer;3     integer i = 5;4     printf("print the int: %d\n", i);5    6     return 0;7 }
 區別於宏定義的概念,typedef相當於定義了一種新的資料類型,而不是簡單的別名 在”別名“的基礎上再定義一個別名    typedef int integer;
    typedef integer mytype; B. typedef與指標
1     typedef char * String;2     String name = "Simon”;
 C. typedef與結構體
1     typedef struct MyPoint2     {3         int x;4         int y;5     } Point;6    7     Point p = {10, 10};
 D. typedef 結構體指標
 1     typedef struct 2     { 3         int age; 4         char *name; 5     } * PP; 6   7 PP per1 = {11, "Tom"};//Running Error
 應該為
 1 int main(int argc, const char * argv[]) { 2     @autoreleasepool { 3         typedef struct{ 4             int age; 5             char *name; 6         } PP,*Person; 7          8         Person p = malloc(sizeof(PP)); 9         p->age = 12;10         p->name = "simon";11         12         printf("%d - %s\n", p->age, p->name);13     }14     return 0;15 }
  E. typedef 指向函數的指標  F. #define 與  typedef 的區別
1 typedef char * String12 #define String2 char *3  4 String1 s1,s2;5 String2 s3,s4;
 ==> 其中,s1,s2,s3是char指標,s4僅僅是普通的char變數

 

  

[C語言 - 9] typedef

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.