c - Is it possible to change the definition of structs at runtime? -
i don't know why ever want this, curious if knew answer. possible @ run time use 1 struct definition while, , later change definition?
i.e.
typedef struct { int a; int b; }my_struct;
and later on...
typedef struct { int a; int b; int c; }my_struct;
no, can't change definition of given type, there's nothing wrong casting totally different type, assuming underlying data laid out , otherwise compatible.
for example, consider:
struct s_xyzzy { int a; int b; }; struct s_plugh { int a; char b0; char b1; char b2; char b3; }; struct s_xyzzy *xyzzy = malloc (sizeof (*xyzzy)); ((struct s_plugh *)xyzzy)->b0 = 'x';
by casting xyzzy
different compatible type, can access fields in different way.
keep in mind compatibility important , have know underlying memory correctly aligned between 2 structures.
you can placing both structures union, using overlapped memory.
Comments
Post a Comment