typedef结构体:typedef使用大全2(结构体)



# S(s) prf("%s\n", #s); s
typedef struct _TS1{
  x, y;
} TS1, *PTS1, ***PPPTS1;  // TS1是结构体名称PTS1是结构体指针名称
// 也就是将结构体struct _TS1 命名为TS1,
// 将struct _TS1 * 命名为 PTS1
// 将struct _TS1 *** 命名为 PPPTS1
 
typedef struct { // struct后面结构体说明也可以去掉
  x, y;
} TS2, *PTS2;
 
typedef PTS1 *PPTS1; // 定义PPTS1是指向PTS1指针
 
typedef struct _TTS1{
 typedef struct ITTS1 {
  x, y;
 } iner;
 iner i;
  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);
 
 prf("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);
 prf("ts2\t = (%d, %d)\n*pts2\t = (%d, %d)\n\n",
  ts2.x, ts2.y, pts2->x, pts2->y);
 pr



Tags:  结构体数组 结构体 结构体的使用 typedef结构体

延伸阅读

最新评论

发表评论