简体   繁体   中英

How to convert uint32_t to struct in_addr?

#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;
}

I don't want to do this by declaring any temporary variable. This program gives segmentation fault.

struct in_addr {
    uint32_t s_addr; 
};

You're casting an int to a pointer. Perhaps you want this:

*(struct in_addr *)&ip

But the result is implementation-defined (for a start, there're endianness considerations).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM