为什么sizeof操作员返回的结构尺寸大于结构成员的总尺寸?
我有以下两种结构:
问题是sizeof(内容)返回160.结构由11个短路,6个整数,76个字符,7个浮点数,1个双精度数组成,总共增加到158个字节.我已计数三次,仍有2个字节的差异.
typedef struct TIME_T {
short year,mon,day;
short hour,min,sec;
} TIME;
typedef struct {
int no;
char name[20];
char Code[10];
char DASType[10];
short wlen;
float VLtd;
int samp;
int comp;
int locationID;
short TranMode;
char TranIns[12];
short TimerMode;
char ClkType[12];
float ClkErr;
float lat;
float lon;
float alt;
float azimuth,incident;
short weight;
short veloc;
int oritype;
char seismometer[12];
double sens;
TIME start_time;
int record_samples;
} Content;
Run Code Online (Sandbox Code Playgroud)
我写了一小段代码来打印结构中每个变量的位置,然后突然发现float wlen需要4个字节.我的代码如下:
int main(void)
{
Content content;
printf("Sizeof Content: %d\n", sizeof(content)); …Run Code Online (Sandbox Code Playgroud)