请帮助我理解最后一行的作用:
int PCKT_LEN = 8192;
char buffer[PCKT_LEN];
struct iphdr *ip = (struct iphdr *) buffer;
struct udphdr *udp = (struct udphdr *) (buffer + sizeof(struct iphdr));
我知道这个,我有点理解:
(struct udphdr *) -> Means cast to a udphdr "object"/instance or something like that.
这个我有疑问/不明白:
(buffer + sizeof(struct iphdr))
This one, I don't quite understand. Is it because it uses the ADDRESS being returned by buffer and then adds the actual size of the buffer until the last byte as an offset before starting to allocate udp memory range? I guess the it wants to start the address of udp right after the ip ?
如果我像下面这样执行最后一行,结果会一样吗?(我将缓冲区改为ip)
struct udphdr *udp = (struct udphdr *) (ip + sizeof(struct iphdr));
谢谢。
问候,
X
(我是 CPP 新手,刚刚开始学习指针和结构体。)
它只是说有一个内存缓冲区
buffer
,其中包含一个iphdr
结构(*ip
),后面紧跟着一个udphdr
结构(*udp
)。第二行将
udp
指针设置为内存中结构结束后恰好一个字节的位置。(它获取基指针,然后将其前进结构在内存中所需的ip
字节数)iphdr
请注意,除了内存之外,绝对没有涉及任何分配
buffer
。代码只是用组成 UDP 数据包的内存结构“覆盖”平面数组。这通常是为了能够从平面缓冲区的明确偏移量中选择字节。(访问它比buffer
访问更有意义)udp->flags
*(buffer + offset)