我有一個結構定義爲:爲什麼offsetof(member)等於sizeof(struct)?
struct smth
{
char a;
int b[];
};
當我打電話sizeof
和offsetof
在這個結構:
cout << sizeof(struct smth) << endl;
cout << offsetof(struct smth, b) << endl;
輸出是:
4
4
如何來時的大小stuct是4並且char使用1個字節,int數組的偏移量是4? 爲什麼會有某種填充? 此外,爲什麼不是int數組佔用任何空間?
你的意思是'offsetof(struct smth,b)',對吧? –
@CarlNorum是的,我喜歡。編輯。 – bugra
它是C還是C++?他們是不同的。 –