Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C:在原始套接字中复制缓冲区中的数据(非常通用)_C_Memcpy - Fatal编程技术网

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。。就这样!!谢谢: