- 论坛徽章:
- 0
|
地址减去一个整数时会发生什么情况
得到“8”结果的源程序:
#include <stdio.h>;
#include <netinet/ip.h>;
#include <netinet/tcp.h>;
#include <netinet/if_ether.h>;
struct header
{
struct ethhdr eth;
struct iphdr ip;
struct tcphdr tcp;
}header_buf;
int main()
{
memset(&header_buf, 0, sizeof(header_buf));
struct iphdr *ip;
struct tcphdr *tcp;
ip = (struct iphdr *)((unsigned long *)&header_buf.ip - 2);
tcp = (struct tcphdr *)((unsigned long *)&header_buf.tcp - 2);
printf("address of header_buf.ip is %x\n", &header_buf.ip);
printf("address of header_buf.tcp is %x\n", &header_buf.tcp);
printf("address of ip is %x\n", ip);
printf("address of tcp is %x\n", tcp);
return 0;
}
打印结果为:
address of header_buf.ip is 8049610
address of header_buf.tcp is 8049624
address of ip is 8049608
address of tcp is 804961c
第三行的值和第一行的值相差8
第四行的值和第二行的值相差8
我用的是red-hat linux,内核2.4.18,gcc 3.2 |
|