#include <iostream>
using namespace std;
struct Student{
char ch;
int i;
}st1;
struct name{
}n;
struct j{
static int i ;
}l;
struct nam{
static int i;
}e;
struct Studen{
}st;
int main(){
int s = sizeof(st1);
int n2 = sizeof(n);
int l = sizeof(l);
int e1 = sizeof(e);
int se = sizeof(st);
printf("%d %d %d %d %d", s, l, n2, e1, se);
return 0;
}
在这个程序中,第二个输出的数字为什么是4呢?不应该是1么?
结构体按照4字节的倍数对齐。
http://www.cnblogs.com/motadou/archive/2009/01/17/1558438.html
http://www.cnblogs.com/longlybits/articles/2385343.html
http://www.cnblogs.com/luxiaoxun/archive/2012/11/09/2762438.html
第二个 输出的 不是 sizeof(l)吗, l位 int类型。 int类型是4字节的
就是1
printf("%d %d %d %d %d", s, l, n2, e1, se);里的l和n2楼主你的顺序写错了吧
对结构体进行sizeof不是单纯的内部变量的的字节和。涉及到内存对齐的问题,具体可以百度。结构体中最长字节不超过4时,默认4字节对齐