public
Created

  • Download Gist
cast_struct.c
C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
#include <stdio.h>
 
struct base {
int foo;
};
 
struct derived {
int foo;
char *bar;
};
 
void foobar(void *ptr)
{
((struct base *) ptr)->foo = 100;
}
 
int main(int argc, char **argv)
{
struct derived d = {1, "derived"};
printf("before: %d\n", d.foo);
foobar(&d);
printf("after: %d\n", d.foo);
return 0;
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.