我也要学C语言-第二十章:结构体类型变量,结构体数组(二)

2014-11-23 22:19:24 · 作者: · 浏览: 14
。其实float也是一种结构体,它只是是以位为单位的。我们先来看1个结构体数组的例子:

struct person{ char name[20]; char sex; int age; float height;};struct person per[3];
哈哈!是不是特好玩!现在我们的数组元素装了个结构体。呵呵!

这里的per是什么呢!per就是person类型的指针常量啦!其实这个和数组差不多了啊!

下面我们来看1个例子程序:

#include

struct stDate

{

int nYear;

int nMonth;

int nDay;

};

struct stStudent{

char szName[64];

char Sex;

struct stDate WoW;

float fScore[3];

};

int main()

{

struct stStudent stu;

printf("%d", (int)stu.fScore - (int)&stu);

return 0;}

注:默认是模4地址的。
这个例子打印出多少呢?!我们一起来数数,64+4+12=80 对吧!呵呵。这个例子是不是就可以求出结构体变量的成员到结构体首地址的偏移量呀!对不对呀!很好玩吧!但是这里有个缺点,必须要用结构体变量,那么我们可以不用一个结构体变量也能求出1个成员的偏移量来吗?!这个问题,到明天的笔记中再说明了。