Skip to content

Instantly share code, notes, and snippets.

@franklindyer
Created August 27, 2023 19:20
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Embed
What would you like to do?
Struct casting example with IP address (little-endian)
#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