struct parent { char a; char b; }; struct child { struct parent parent; int c; char d; }; struct grandchild { struct child child; long e; }; void print_parent_val(struct parent *p) { printf("%d\n", p->a); } int main (int argc, char **argv) { struct grandchild g; g.child.parent.a = 69; print_parent_val((struct parent *)&g); return 0; }
The program compiles (without warning) and works fine and prints 69 as expected. I have never seen code that implements this inheritance technology, so I really hesitate to see this as "OK."
EDIT: How about turning a grandson into a child? Is transformation of the middle generation possible?
void print_parent_val(struct child *c) { printf("%c\n", c->d); } int main (int argc, char **argv) { struct grandchild g; g.child.parent.a = 69; g.child.d = 'w'; print_parent_val((struct child *)&g); return 0; }
c language-lawyer struct
Sanchke dellowar
source share