|
a和b是把ip地址转为整数。只是t1,t2,t3,t4的赋值顺序不通。
执行a和b后,输入相同的值,结果缺不通,a是正确的。
输入0.0.1.0结果应为256
vi a.c
union trans {
unsigned int i;
unsigned char c[4];
};
main() {
union trans t;
unsigned char t1,t2,t3,t4;
scanf("%d.%d.%d.%d",&t4,&t3,&t2,&t1);
t.c[3]=t4;
t.c[2]=t3;
t.c[1]=t2;
t.c[0]=t1;
printf("int is %d\n",t.i);
}
vi b.c
union trans {
unsigned int i;
unsigned char c[4];
};
main() {
union trans t;
unsigned char t1,t2,t3,t4;
printf("input ip: ");
scanf("%d.%d.%d.%d",&t1,&t2,&t3,&t4);
t.c[3]=t4;
t.c[2]=t3;
t.c[1]=t2;
t.c[0]=t1;
printf("int is %d\n",t.i);
} |
|