Struct casting example with IP address (little-endian)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdint.h> | |
struct ip4 { | |
uint8_t first; | |
uint8_t second; | |
uint8_t third; | |
uint8_t fourth; | |
}; | |
int main() { | |
uint32_t whole_ip = -2; // IP address 255.255.255.254 | |
struct ip4* split_ip = (struct ip4*)(&whole_ip); | |
printf("%d.%d.%d.%d\n", split_ip->fourth, split_ip->third, split_ip->second, split_ip->first); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment