Created
November 9, 2010 06:02
-
-
Save banister/668772 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 <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