#define
s(s) printf("%s/n", #s); s
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);
}