C語言 結構體大小計算
/*個人標籤啥東東封尾機*/
參考:http://blog.sina.com.cn/s/blog_5f77c7270100dnoy.html
實際上,由於存儲變量時地址對齊的要求,編譯器在編譯程序時會遵循兩條原則:
結構體大小:相同類型的數據聲明結束後,下一個成員的偏移必須爲成員大小的整數倍。
eg:
typedef struct stu4
{
char c1;
char c2;
char c3;
int i;
}stu4;
typedef struct stu5
{
char c1;
char c2;
char c3;
char c4;
int i;
}stu4;
這兩個結構體大小都爲8(已驗證)。
二、結構體大小必須是所有成員大小的整數倍。
vs2010
poC:在main.c中 如果按照如下聲明就使用的話,包未聲明的標示符錯誤。如果是typedef struct stu4{}stu4;形式正確。爲毛?????
struct stu4
{
char c1;
char c2;
char c3;
int i;
};
----------------------------不爲毛。
使用時如果沒有typedef struct stu4{}stu4。。。。。。。。。的話,使用時必須在stu4前標示struct。
poC
20130913------------------------
C機構體中不可以有函數,包括析構函數。