【问题标题】:Printing the return value from inet_aton() function从 inet_aton() 函数打印返回值
【发布时间】:2015-04-05 15:19:51
【问题描述】:

我有这个工作正常的代码:

#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
    struct in_addr addr;

    if (argc != 2) {
        fprintf(stderr, "%s <dotted-address>\n", argv[0]);
        exit(EXIT_FAILURE);
    }

    if (inet_aton(argv[1], &addr) == 0) {
        perror("inet_aton");
        exit(EXIT_FAILURE);
    }

    printf("%s\n", inet_ntoa(addr));
    exit(EXIT_SUCCESS);
}

我想要实现的是打印inet_aton()函数的值。函数的描述说它返回一个数字,但是当我尝试打印它时,它说“不能从地址结构转换为十进制”。

【问题讨论】:

  • addr.s_addr 应该是一个无符号整数。参见例如man7.org/linux/man-pages/man7/ip.7.html
  • 在哪里你得到错误?您能否编辑您的问题以包含更多详细信息(例如,如果它是编译器错误或运行时错误,actualcomplete 错误)。
  • 这是一个编译器错误。

标签: c network-programming inet-aton


【解决方案1】:

使用这个而不是你最后一个 printf 为我工作:

printf("%d\n", addr.s_addr);

【讨论】:

  • 如果你只做printf("%d\n", addr);,你会得到一个编译器警告(因为这是错误的形式),但它仍然可以工作。
猜你喜欢
  • 2020-01-26
  • 2019-10-14
  • 2020-01-29
  • 2015-12-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-03
相关资源
最近更新 更多