注册 登录
编程论坛 C++教室

请假sizeof问题

youtk21ai 发布于 2010-06-06 03:19, 423 次点击
#include "iostream.h"
#include "iomanip.h"

struct  student
{
    char id[10];
    char name[20];
    char sex;
    int age;
    char department[30];
    float score;
};

void main(){
    student student1 = {"05030100","WangWei",'M',20,"Computer",95},
        student2 = {"05030101","LiMing",'M',21,"Physic",98};
    float average;
    cout<<student1.age<<" "<<student1.department<<" "<<student1.id<<" "<<student1.name<<" "<<student1.score<<" "<<student1.sex<<endl;
    cout<<student2.age<<" "<<student2.department<<" "<<student2.id<<" "<<student2.name<<" "<<student2.score<<" "<<student2.sex<<endl;
    average = (student1.score+student2.score)/2;
    cout<<"average="<<average;
    cout<<"sizeofstudent1="<<sizeof(student1)<<endl;
}

为什么sizeof(student1)=72?
1 回复
#2
迷失的木桶2010-06-06 08:13
字节对齐而已,默认4的整倍数,谷歌一下多的是,呵呵
1