(简单)宏定义 与 用户自定义类型typedef

来源:互联网 发布:excel 数据清洗 编辑:程序博客网 时间:2024/05/17 02:47

宏定义(宏替换)

    #define   标识符   被替换信息

例:<1> #define   P   printf("

        ......

        P%d,%d",a,b); //printf("%d,%d",a,b);

       ......

    <2> #define   Max(x,y)   ((x)>(y)?(x):(y))

    <3> #define   TURE   1

        #define   FALSE   0

  

    总结:1.宏替换不能改变运算量

          2.提高程序的可维护性

          3.提高程序的可读性


用户自定义类型  typedef

    #define   已有类型   新类型名称

例: typedef   int   Integer; //int  a; <==> Integer  a;

     typedef   struct   Student_Information   ST;


   typedef常与struct联合使用,以简化结构体类型的书写!

          Typedef   struct   STINF{

               ............

           }STINF;//结构体实例名称

                      STINF   a ,b[50] ,*p; // <==> struct   STINF   a ,b[50] ,*p;


用户自定义类型typedef 延伸

     typedef   char   *CHP <==> typedef   char*   CHP

     typedef   char   CHA[10] <==> typedef   char[10]   CHA


         CHP   a1 ,*b1 ,c1[10];<==> char   *a1; char   **b1; char   *c1[10];

         // CHP  c1[10]:  char*  c1[10] <==>char  *c1[10]指针数组


         CHA   a2 ,*b2 ,c2[20];<==> char   a2[10]; char   *b2[10]; char   c2[20][10];

       // CHA  *b2:  char[10]  *b2 <==> char  *b2[10] <==> char*  b2[10] { 错误 }

                       char[10]  *b2 <==>char  (*b2)[10]数组指针<==> char[10]  *b2{ 正确 }

       // CHA  c2[20]:  char[10]  c2[20] <==> char  c2[10][20] <==> char[20]  c2[10]{ 错误 }

                         char[10]  c2[20] <==> char  c2[20][10] <==> char[10]  c2[20]{ 正确 }

    综上:typedef语句可以“构造”复杂的数据类型!


#define语句 和 typedef语句

         

          #define   X   char*

            typedef   char   *Y

                 X   a ,b ,c; <==>char   *a ,b ,c;

                 Y   a ,b ,c; <==>char   *a ,*b ,*c;

              


                      





       


     


原创粉丝点击