Suppose I have two structs, one of which can be considered an extension of another... is it safe to do this:
Code:
typedef struct _original
{
int field1;
double field2;
} original;
typedef struct _extended
{
int field1;
double field2;
char * metadata;
} extended;
int main(int argc, char ** argv)
{
original * org = malloc(sizeof(original));
org->field1 = 10;
org->field2 = 11.11;
extended * ext = (extended *)org;
ext->metadata = "metadata";
printf("%d %lf %s\n",ext->field1, ext->field2, ext->metadata);
}
The print shows that the new struct ext has all of the right information.
I guess the other way would be to copy the information directly, but such memory copies cause unnecessary slowdown.
Is it OK for me to do the casting that way?