sizeof 结构体(struct)和 联合体(union)的大小是不一样的
char short int long
#include <stdio.h>
typedef unsigned char u_char ;
typedef unsigned short u_short;
typedef unsigned long u_long ;
typedef unsigned int u_int ;
struct in_addr
{
union {
struct { u_char s_b1,s_b2,s_b3,s_b4; } S_un_b;
struct { u_short s_w1,s_w2; } S_un_w;
u_long S_addr;
} S_un;
};
struct sockaddr_in {
short sin_family;
u_short sin_port;
struct in_addr sin_addr;
char sin_zero[8];
};
struct x1
{
int a1;
int b1;
int c1;
};
union x2
{
int a1;
int b1;
int c1;
};
void main()
{
printf("%d\n",sizeof(in_addr)); // 4
printf("%d\n",sizeof(sockaddr_in)); // 16
printf("%d\n",sizeof(x1)); // 12
printf("%d\n\n",sizeof(x2)); // 4
printf("%d\n",sizeof(char)); // 1
printf("%d\n",sizeof(short)); // 2
printf("%d\n",sizeof(int)); // 4
printf("%d\n\n",sizeof(long)); // 4
printf("%d\n",sizeof(u_char)); // 1
printf("%d\n",sizeof(u_short)); // 2
printf("%d\n",sizeof(u_int)); // 4
printf("%d\n",sizeof(u_long)); // 4
printf("%d\n",sizeof(float)); // 4
printf("%d\n",sizeof(double)); // 8
}