1. 程式人生 > >黑馬程式設計師-C-關鍵字typedef

黑馬程式設計師-C-關鍵字typedef

1.作用:給已經存在的型別起一個新的名字

2.適用場合

     a.基本資料型別

     b.指標

     c.結構體

     d.列舉

     e. 指向函式的指標

#include <stdio.h>

typedef int MyInt ;

typedef MyInt MyInt2;

// 給指標型別char *起一個新的型別名稱String

typedef char * String ;

struct Student

{

   int age ;

};

typedef struct Student MyStu ;

typedef enum Sex

{

   Man;

   Woman ;

} MySex ;

typedef int (* Mypoint ) (int , int );

int minus (int a, int b)

{

   return a- b;

}

int sum (int a , int b)

{

   return a+b;

}

typedef struct Person

{

   int age ;

} * PersonPoint ;

int main ()

{

  //定義結構體變數

  struct Person p = {20};

 PersonPoint p2 =&p;

   return 0 ;

}

3.typedef 使用注意

#define Integer int 和 typedef int Integer;意思是一樣的