gpt4 book ai didi

c - 如何将 uint32_t 转换为 struct in_addr?

转载 作者:太空狗 更新时间:2023-10-29 17:20:47 25 4
gpt4 key购买 nike

#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

int main()
{
uint32_t ip = 0;
printf("%s\n",inet_ntoa(*(struct in_addr *)ip));
return 0;
}

我不想通过声明任何临时变量来做到这一点。该程序给出了段错误。

struct in_addr {
uint32_t s_addr;
};

最佳答案

您正在将 int 转换为指针。也许你想要这个:

*(struct in_addr *)&ip

但结果是实现定义的(首先,需要考虑字节顺序)。

关于c - 如何将 uint32_t 转换为 struct in_addr?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9961056/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com