Skip to content

Instantly share code, notes, and snippets.

@franklindyer
Created August 27, 2023 19:20
Show Gist options
  • Save franklindyer/206daab8a0f878881b5d46fc79e5696e to your computer and use it in GitHub Desktop.
Save franklindyer/206daab8a0f878881b5d46fc79e5696e to your computer and use it in GitHub Desktop.
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