【发布时间】:2014-06-23 05:27:56
【问题描述】:
我已将联合定义为
union person
{
int roll;
char fname[10];
char lname[20];
}p1;
并且sizeof(p1)=20字节。 但是在将内容存储在 p1 中时,它存储了 20 多个字符。
void main()
{
printf("\n The size of the union is %zu\t",sizeof(p1));
printf("\n Enter the details here :\t");
scanf("%d",&p1.roll);
scanf("%s",p1.fname);
scanf("%s",p1.lname);
printf("\n The union contains are \n");
printf("\n The roll no is :: \t %d",p1.roll);
printf("\n The fname is :: \t %s ",p1.fname);
printf("\n The laname is :: \t %s\n",p1.lname);
}
当我输入超过 20 个字符时,它仍然被存储。
【问题讨论】:
标签: c memory-management struct unions