sizeof和unsigned char数组错误? (C / C ++)

时间:2012-02-12 19:57:51

标签: c++ char sizeof unsigned

我写过这个结构:

struct bmpheader {
    unsigned char magic[2];
    unsigned int fsize;
    unsigned int unused;
    unsigned int pixdata_offset;
    unsigned int headersize;
    unsigned int width;
    unsigned int height;
    unsigned short planes_color;
    unsigned short bpp;
    unsigned int compression;
    unsigned int sizeofpix;
    unsigned int resolutionx;
    unsigned int resolutiony;
    unsigned int colors;
    unsigned int imp_colors;
};

我的sizeof功能有问题。当我指望我的手指,它每次54个字节对我来说。 sizeof函数每次给我56.为什么?问题是unsigned char数组,当我删除magic [2]数组时,sizeof是52? sizeof(header.magic)显示为2.据我所知,unsigned char是1字节类型?

提前感谢您的回复。

的Marcin

1 个答案:

答案 0 :(得分:1)

因为int(在您的机器上)必须是,或者在放置在4字节地址上时效率更高。编译器将在char数组后插入两个填充字节。