指向libpcap中数据包len的指针

指向libpcap中数据包len的指针,c,pointers,libpcap,C,Pointers,Libpcap,在libpcap中,我有这个用于嗅探和打印数据包长度的代码 int main(int argc, char *argv[]) { pcap_t *handle; /* Session handle */ char *dev; /* The device to sniff on */ char errbuf[PCAP_ERRBUF_SIZE]; /* Error string */ struct bpf_program fp;

libpcap
中,我有这个用于嗅探和打印数据包长度的代码

int main(int argc, char *argv[])
 {
    pcap_t *handle;         /* Session handle */
    char *dev;          /* The device to sniff on */
    char errbuf[PCAP_ERRBUF_SIZE];  /* Error string */
    struct bpf_program fp;      /* The compiled filter */
    char filter_exp[] = "port 23";  /* The filter expression */
    bpf_u_int32 mask;       /* Our netmask */
    bpf_u_int32 net;        /* Our IP */
    struct pcap_pkthdr header;  /* The header that pcap gives us */
    const u_char *packet;       /* The actual packet */

    /* Define the device */
    dev = pcap_lookupdev(errbuf);
    if (dev == NULL) {
        fprintf(stderr, "Couldn't find default device: %s\n", errbuf);
        return(2);
    }
    /* Find the properties for the device */
    if (pcap_lookupnet(dev, &net, &mask, errbuf) == -1) {
        fprintf(stderr, "Couldn't get netmask for device %s: %s\n", dev, errbuf);
        net = 0;
        mask = 0;
    }
    
    /* Open the session in promiscuous mode */
    handle = pcap_open_live(dev, BUFSIZ, 1, 1000, errbuf);
    if (handle == NULL) {
        fprintf(stderr, "Couldn't open device %s: %s\n", dev, errbuf);
        return(2);
    }
    /* Compile and apply the filter */
    if (pcap_compile(handle, &fp, filter_exp, 0, net) == -1) {
        fprintf(stderr, "Couldn't parse filter %s: %s\n", filter_exp, pcap_geterr(handle));
        return(2);
    }
    if (pcap_setfilter(handle, &fp) == -1) {
        fprintf(stderr, "Couldn't install filter %s: %s\n", filter_exp, pcap_geterr(handle));
        return(2);
    }
    while(1)
    {
        packet = pcap_next(handle, &header);
        printf("packet len =  [%d]\n", header.len);
    }
    pcap_close(handle);
    return(0);
 }
我想在循环之前设置指向
header.len
的指针,并在每次迭代中打印它:

bpf_u_int32 * len= &header.len
while(1)
{
    packet = pcap_next(handle, &header);
    printf("packet len =  [%d]\n", *len);
}

在每次迭代中,
header.len
的地址会改变吗?

在该循环中,
header
的地址不会改变

然而,没有理由这么做

bpf_u_int32 * len= &header.len
while(1)
{
    packet = pcap_next(handle, &header);
    printf("packet len =  [%d]\n", *len);
}
而不仅仅是

while(1)
{
    packet = pcap_next(handle, &header);
    printf("packet len =  [%d]\n", header.len);
}
您将得到相同的答案,事实上,编译器甚至可能为此生成相同的代码。(这不是你祖父的C;C编译器比以前做了更多的优化和其他代码转换。)


将指向
header.len
的指针放入变量中,并取消对指针的引用,从本质上讲并不更有效。例如,如果“加载/推送寄存器指向的对象”比“从寄存器指向的偏移量加载/推送对象”更有效,编译器可能会生成代码来实现这一点。

为什么需要另一个指向
header.len的指针?直接引用它有什么不对?