Closed. This question is off-topic. It is not currently accepting answers. Learn more
想改进这个问题吗?Update the question所以堆栈溢出的值小于aa>。
我在学习c语言中的ping源代码。
在这篇文章里,我被看了一行。这是if条件,后跟while(0)。
我在网上被搜过。他们给出的行只跟在while(0)后面。因为do后面跟着while(0),我知道答案。但我不知道情况是否在while(0)之后。
示例代码是,

if (source.sin_addr.s_addr == 0) {
    socklen_t alen;
    struct sockaddr_in dst = whereto;
    int probe_fd = socket(AF_INET, SOCK_DGRAM, 0);

    if (probe_fd < 0) {
        perror("socket");
        exit(2);
    }
    if (device) {
        struct ifreq ifr;
        int rc;

        memset(&ifr, 0, sizeof(ifr));
        strncpy(ifr.ifr_name, device, IFNAMSIZ-1);

        enable_capability_raw();
        rc = setsockopt(probe_fd, SOL_SOCKET, SO_BINDTODEVICE, device, strlen(device)+1);
        disable_capability_raw();

        if (rc == -1) {
            if (IN_MULTICAST(ntohl(dst.sin_addr.s_addr))) {
                struct ip_mreqn imr;
                if (ioctl(probe_fd, SIOCGIFINDEX, &ifr) < 0) {
                    fprintf(stderr, "ping: unknown iface %s\n", device);
                    exit(2);
                }
                memset(&imr, 0, sizeof(imr));
                imr.imr_ifindex = ifr.ifr_ifindex;
                if (setsockopt(probe_fd, SOL_IP, IP_MULTICAST_IF, &imr, sizeof(imr)) == -1) {
                    perror("ping: IP_MULTICAST_IF");
                    exit(2);
                }
            } else {
                perror("ping: SO_BINDTODEVICE");
                exit(2);
            }
        }
    }

    if (settos &&
        setsockopt(probe_fd, IPPROTO_IP, IP_TOS, (char *)&settos, sizeof(int)) < 0)
        perror("Warning: error setting QOS sockopts");

    dst.sin_port = htons(1025);
    if (nroute)
        dst.sin_addr.s_addr = route[0];
    if (connect(probe_fd, (struct sockaddr*)&dst, sizeof(dst)) == -1) {
        if (errno == EACCES) {
            if (broadcast_pings == 0) {
                fprintf(stderr, "Do you want to ping broadcast? Then -b\n");
                exit(2);
            }
            fprintf(stderr, "WARNING: pinging broadcast address\n");
            if (setsockopt(probe_fd, SOL_SOCKET, SO_BROADCAST,
                       &broadcast_pings, sizeof(broadcast_pings)) < 0) {
                perror ("can't set broadcasting");
                exit(2);
            }
            if (connect(probe_fd, (struct sockaddr*)&dst, sizeof(dst)) == -1) {
                perror("connect");
                exit(2);
            }
        } else {
            perror("connect");
            exit(2);
        }
    }
    alen = sizeof(source);
    if (getsockname(probe_fd, (struct sockaddr*)&source, &alen) == -1) {
        perror("getsockname");
        exit(2);
    }
    source.sin_port = 0;

#ifndef WITHOUT_IFADDRS
    if (device) {
        struct ifaddrs *ifa0, *ifa;
        int ret;

        ret = getifaddrs(&ifa0);
        if (ret) {
            fprintf(stderr, "gatifaddrs() failed.\n");
            exit(2);
        }
        for (ifa = ifa0; ifa; ifa = ifa->ifa_next) {
            if (!ifa->ifa_addr || ifa->ifa_addr->sa_family != AF_INET)
                continue;
            if (!strncmp(ifa->ifa_name, device, sizeof(device) - 1) &&
                !memcmp(&((struct sockaddr_in *)ifa->ifa_addr)->sin_addr,
                    &source.sin_addr, sizeof(source.sin_addr)))
                break;
        }
        freeifaddrs(ifa0);
        if (!ifa)
            fprintf(stderr, "ping: Warning: source address might be selected on device other than %s.\n", device);
    }
#endif
    close(probe_fd);
} while (0);

在此after if条件下,它们使用while(0)。这有什么用。
有人能给我解释一下吗?

最佳答案

你在读代码的时候欺骗了自己。

if (condition) {
    …
}

while (0)
    ;

这就是代码的真正含义。while循环是完全无用的(它什么也不做),我无法告诉您它可能存在的任何原因。
为了解决这个问题,让你的代码自动格式化。

07-24 09:45
查看更多