void ip2s(int n)
{
unsigned char *p = &n;
printf(“%u.%u.%u.%u\n”, *p, *(p + 1), *(p + 2), *(p + 3));
}
int main()
{
int ip=0;
unsigned char *p1=&ip;
int a=0x12345678;
char*p=&a;
p++;
printf(“%x\n”,a);
//”192.168.6.252″
*p1=192;
*p1=168;
p1+=1;
*p1=6;
p1+=2;
*p1=252;
p1+=3;
printf(“ip=%d\n”,ip);
ip2s(ip);
return 0;
}
本人想输出出来是192.168.6.252但是输出是168.6.252.0这是什么回事啊?
{
unsigned char *p = &n;
printf(“%u.%u.%u.%u\n”, *p, *(p + 1), *(p + 2), *(p + 3));
}
int main()
{
int ip=0;
unsigned char *p1=&ip;
int a=0x12345678;
char*p=&a;
p++;
printf(“%x\n”,a);
//”192.168.6.252″
*p1=192;
*p1=168;
p1+=1;
*p1=6;
p1+=2;
*p1=252;
p1+=3;
printf(“ip=%d\n”,ip);
ip2s(ip);
return 0;
}
本人想输出出来是192.168.6.252但是输出是168.6.252.0这是什么回事啊?
解决方案
60
你给p1赋值的时候用的不对,每次+=1就行了
#include<stdio.h> void ip2s(int n) { unsigned char *p = (unsigned char *)&n; printf("%u.%u.%u.%u\n", *p, *(p + 1), *(p + 2), *(p + 3)); } int main() { int ip=0; unsigned char *p1=(unsigned char *)&ip; int a=0x12345678; char*p=(char *)&a; p++; printf("%x\n",a); //"192.168.6.252" *p1=192; p1+=1; *p1=168; p1+=1; *p1=6; p1+=1; *p1=252; printf("ip=%d\n",ip); ip2s(ip); return 0; }
10
*p1=192; p1+=1;*p1=168; p1+=1;*p1=6; p1+=1;*p1=252;