回复 2楼 lmlm1001
我把每个成员的大小打印出来,运行结果是20,4,1,4
把每个成员的地址也打印出来,发现char cSex 和 int iGrade的地址差距为4,char cSex大小为1,为何占了4个字节。
程序代码:
#include <stdio.h>
int main()
{
struct Student
{
char cName[20];
int iNumber;
char cSex;
int iGrade;
};
struct Student student[5]=
{
{"Wangjiasheng",12062212,'M',3},
{"YuLongjiao",12062213,'W',3},
{"JiangXuehuan",12062214,'W',3},
{"ZhangMeng",12062215,'W',3},
{"HanLiang",12062216,'M',3},
};
printf("sizeof(student[0].cName)=%d\n",sizeof(student[0].cName));
printf("sizeof(student[0].iNumber)=%d\n",sizeof(student[0].iNumber));
printf("sizeof(student[0].cSex)=%d\n",sizeof(student[0].cSex));
printf("sizeof(student[0].iGrade)=%d\n",sizeof(student[0].iGrade));
printf("locationof(student[0].cName)=%p\n",&student[0].cName);
printf("locationof(student[0].iNumber)=%p\n",&student[0].iNumber);
printf("locationof(student[0].cSex)=%p\n",&student[0].cSex);
printf("locationof(student[0].iGrade)=%p\n",&student[0].iGrade);
return 0;
}
图片附件: 游客没有浏览图片的权限,请
登录 或
注册