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 do not want to do this by declaring a temporary variable. This program gives a segmentation error.

 struct in_addr { uint32_t s_addr; }; 
+6
source share
1 answer

You type int in a pointer. Perhaps you need this:

 *(struct in_addr *)&ip 

But the result is determined by the implementation (for starters, there are considerations regarding judgment).

+9
source

Source: https://habr.com/ru/post/912157/


All Articles