#include<iostream>
using namespace std;
//最大元素长度
union A{
char a;
int b;
double c;
double d;
};
struct B{
char a;
char d;
double b;
char c;
};
struct C{
char a;
char d;
int b;
char c;
};
//按照2字节来对齐,默认为4
#pragma pack(2)
struct D{
char a;
char b;
int c;
char d;
};
void func(char ch[])
{
cout<<sizeof(ch)<<endl;
}
int main()
{
A a;
B b;
C c;
D d;
char ch[] = "love";
cout<<sizeof(a)<<endl;
cout<<sizeof(b)<<endl;
cout<<sizeof(c)<<endl;
cout<<sizeof(d)<<endl;
cout<<sizeof(ch)<<endl;
func(ch);
return 0;
}
输出:
8
24
12
8
5
8
Program ended with exit code: 0