原创 typedef 使用大全2(结构体)

2008-7-2 23:38 2344 1 1 分类: MCU/ 嵌入式

typedef 使用大全2(结构体)


#define S(s) printf("%s\n", #s); s<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />



 


typedef struct _TS1{


    int x, y;


} TS1, *PTS1, ***PPPTS1;  // TS1是结构体的名称,PTS1是结构体指针的名称


// 也就是将结构体struct _TS1 命名为TS1,


// struct _TS1 * 命名为 PTS1


// struct _TS1 *** 命名为 PPPTS1


 


typedef struct { // struct后面的结构体说明也可以去掉


    int x, y;


} TS2, *PTS2;


 


typedef PTS1 *PPTS1; // 定义PPTS1是指向PTS1的指针


 


typedef struct _TTS1{


    typedef struct ITTS1 {


        int x, y;


    } iner;


    iner i;


    int x, y;


} TTS1;


 


//结构体内部的结构体也一样可以定义


typedef TTS1::ITTS1 ITS1;


 


void test_struct()


{


    // 基本结构体重定义的使用


    TS1 ts1 = {100, 200};


    PTS1 pts1 = &ts1; // 完全等价于TS1* pts1 = &ts1;


    PPTS1 ppts1 = &pts1; // 完全等价于TS1** ppts1 = &pts1;


    PPPTS1 pppts1 = &ppts1; // 完全等价于 TS1*** pppts1 = &ppts1;


 


    TS2 ts2 = {99, 88};


    PTS2 pts2 = &ts2;   // 完全等价于 TS2* pts2 = &ts2;


 


    TTS1 itts1 = {{110, 220}, 10, 20};


    Its1* rits1 = &itts1.i;


    ITS1* &its1 = rits1; // 等价于 TTS1::ITTS1 *its1 = &(itts1.i);


 


    printf("ts1\t = (%d, %d)\n*pts1\t = (%d, %d)\n"


           "**ppts1\t = (%d, %d)\n***pppts1= (%d, %d)\n\n",


            ts1.x, ts1.y, pts1->x, pts1->y,


            (**ppts1).x, (**ppts1).y, (***pppts1).x, (***pppts1).y);


    printf("ts2\t = (%d, %d)\n*pts2\t = (%d, %d)\n\n",


        ts2.x, ts2.y, pts2->x, pts2->y);


    printf("itts1\t = [(%d, %d), %d, %d]\n*its1\t =  (%d, %d)\n\n",


        itts1.i.x, itts1.i.y, itts1.x, itts1.y, its1->x, its1->y);


 


    S(pts1->x = 119);


    S(pts2->y = 911);


    S(its1->x = 999);


 


    printf("ts1\t = (%d, %d)\n*pts1\t = (%d, %d)\n"


           "**ppts1\t = (%d, %d)\n***pppts1= (%d, %d)\n\n",


            ts1.x, ts1.y, pts1->x, pts1->y,


            (**ppts1).x, (**ppts1).y, (***pppts1).x, (***pppts1).y);


    printf("ts2\t = (%d, %d)\n*pts2\t = (%d, %d)\n\n",


        ts2.x, ts2.y, pts2->x, pts2->y);


    printf("itts1\t = [(%d, %d), %d, %d]\n*its1\t =  (%d, %d)\n\n",


        itts1.i.x, itts1.i.y, itts1.x, itts1.y, its1->x, its1->y);


 


    S((*ppts1)->y = -9999);


    printf("ts1\t = (%d, %d)\n**ppts1\t = (%d, %d)\n\n",


        ts1.x, ts1.y, (*ppts1)->x, (*ppts1)->y);


 


    S((**pppts1)->x = -12345);


    S((***pppts1).y = -67890);


    printf("ts1\t = (%d, %d)\n*pts1\t = (%d, %d)\n"


           "**ppts1\t = (%d, %d)\n***pppts1= (%d, %d)\n\n",


            ts1.x, ts1.y, pts1->x, pts1->y,


            (**ppts1).x, (**ppts1).y, (***pppts1).x, (***pppts1).y);


}

PARTNER CONTENT

文章评论0条评论)

登录后参与讨论
我要评论
0
1
关闭 站长推荐上一条 /2 下一条