C:在原始套接字中复制缓冲区中的数据(非常通用)
我使用的是原始套接字。我正确地收到了包裹C:在原始套接字中复制缓冲区中的数据(非常通用),c,memcpy,C,Memcpy,我使用的是原始套接字。我正确地收到了包裹 unsigned char *rbuffer, ch, *packet; rbuffer=malloc(22); ch=*(packet+sizeof(struct ethhdr)+sizeof(struct iphdr)+2); printf("\tpacket_buffer[1]=%d",ch); //printing right as sent memcpy(rbuffer,packet+sizeof(struct iphdr)+sizeo
unsigned char *rbuffer, ch, *packet;
rbuffer=malloc(22);
ch=*(packet+sizeof(struct ethhdr)+sizeof(struct iphdr)+2);
printf("\tpacket_buffer[1]=%d",ch); //printing right as sent
memcpy(rbuffer,packet+sizeof(struct iphdr)+sizeof(struct iphdr)+1,22);
printf("\nrbuffer[1]=%d",rbuffer[1]); //printing wrong
为什么会这样
有什么帮助吗
谢谢:因为%d将您的无符号字符指针视为指向整数的指针,导致您的数字与预期不符?对与错是非常未定义的术语。您正在打印一个带有%d的字符,这需要一个int.@o_weisman,它也在第一个printf中!!您将通过packet+sizeofstruct ethhdr+在第一个中获得偏移量。。。第二个是packet+sizeofstruct-iphdr+..@Jayesh-yes。。就这样!!谢谢: