#include "stdio.h"
struct HAR
{int x;int y;
struct HAR *p;
} h[2];
void main()
{h[0].x=1;h[0].y=2;
h[1].x=3;h[1].y=4;
h[0].p=h[0].p=h;
printf("%d %d\n",(h[0].p)->x,(h[1].p)->y);
}
输出结果是?
#include "stdio.h"
struct HAR
{int x;int y;
struct HAR *p;
} h[2];
void main()
{h[0].x=1;h[0].y=2;
h[1].x=3;h[1].y=4;
h[0].p=(struct HAR*)malloc(sizeof(struct HAR));
h[1].p=(struct HAR*)malloc(sizeof(struct HAR));
h[0].p=h[1].p=h;
printf("%d %d\n",(h[0].p)->x,(h[1].p)->y);
system("pause");
}
在你的基础上改了一下,因为p是指针,使用前必需为其分配空间。因为h[0].p=h[1].p=h;其实h[0].p和h[1].p都指向h[]数组的头一个节点,即h[0]所以结果是1和2.
结果是: