【學習筆記】【C語言】typedef,學習筆記typedef

來源:互聯網
上載者:User

【學習筆記】【C語言】typedef,學習筆記typedef

1.概念

我們可以使用typedef關鍵字為各種資料類型定義一個新名字(別名)。

2.作用:給已經存在的類型起一個新的名稱

3.使用場合:
 1> 基礎資料型別 (Elementary Data Type)
 2> 指標
 3> 結構體
 4> 枚舉
 5> 指向函數的指標

4.代碼

  1 #include <stdio.h>  2   3 typedef int MyInt;  4 typedef MyInt MyInt2;  5   6 // 給指標類型char *起一個新的類型名稱String  7 typedef char * String;  8   9 /* 10 struct Student 11 { 12     int age; 13 }; 14 typedef struct Student MyStu; 15 */ 16  17 /* 18 typedef  struct Student 19 { 20     int age; 21 } MyStu; 22 */ 23  24  25 typedef struct 26 { 27     int age; 28 } MyStu; 29  30 /* 31 enum Sex {Man, Woman}; 32 typedef enum Sex MySex; 33 */ 34  35 typedef enum { 36     Man, 37     Woman 38 } MySex; 39  40  41 typedef int (*MyPoint)(int, int); 42  43 int minus(int a, int b) 44 { 45     return a - b; 46 } 47  48 int sum(int a, int b) 49 { 50     return a + b; 51 } 52 /* 53 struct Person 54 { 55     int age; 56 }; 57  58 typedef struct Person * PersonPoint; 59 */ 60  61 typedef struct Person 62 { 63     int age; 64 } * PersonPoint; 65  66 int main() 67 { 68     // 定義結構體變數 69     struct Person p = {20}; 70      71     PersonPoint p2 = &p; 72      73     //struct Person *p2 = &p; 74      75     //MyPoint p = sum; 76     //MyPoint p2 = minus; 77     //int (*p)(int, int) = sum; 78      79     //int (*p2)(int, int) = minus; 80      81     //p(10, 11); 82      83      84     //MySex s = Man; 85     //enum Sex s = Man; 86     //enum Sex s2 = Woman; 87      88    // struct Student stu3; 89     //MyStu stu = {20}; 90     //MyStu stu2= {21}; 91      92     return 0; 93 } 94  95 void test2() 96 { 97     String name = "jack"; 98      99     printf("%s\n", name);100 }101 102 void test()103 {104     int a;105     MyInt i = 10;106     MyInt2 c = 20;107     108     MyInt b1, b2;109     110     printf("c is %d\n", c);111 }

使用注意

 1 #include <stdio.h> 2  3 //#define Integer int 4  5 //typedef int Integer; 6  7 //typedef unsigned long int MyInt; 8  9 #define String2 char *10 11 typedef char * String;12 13 int main()14 {15     /*16     int a,b;17     int a;18     int b;19     */20     21     //s1、s2是char *指標22     String s1, s2;23     /*24      String s1;25      String s2;26      */27     s1 = "jack";28     s2 = "rose";29     30     // s3才是char *指標,s4隻是char31     String2 s3, s4;32     /*33     char *s3, s4;34     char *s3;35     char s4;36     */37     //String2 s3 = "jake";38     39     /*40     String s1;41     String s2;42     */43     44     45     46     47     //Integer i = 10;48     49     return 0;50 }

 

 

聯繫我們

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