Skip to content

Instantly share code, notes, and snippets.

@banister
Created November 9, 2010 06:02
Show Gist options
  • Save banister/668772 to your computer and use it in GitHub Desktop.
Save banister/668772 to your computer and use it in GitHub Desktop.
#include <stddef.h>
#include <stdio.h>
typedef struct mystruct {
int x, y;
double f;
char * hello;
} mystruct;
void
recover_struct(double * f)
{
struct mystruct butter;
butter = *(mystruct *)((void*)(f) - offsetof(mystruct, f));
printf("%s\n", butter.hello);
printf("%d\n", butter.x);
printf("%f\n", butter.f);
}
int
main(void)
{
mystruct s = {.x = 10, .f = -66.6, .hello = "john" };
double * f = &s.f;
recover_struct(f);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment