Originally Posted by
dayalsoap
Suppose I have two structs, one of which can be considered an extension of another... is it safe to do this:
One soution to this problem is to use a union... (You will see this in windows occasionally where they've changed a struct)
Code:
typedef union torig
{ int field1;
double field2;
struct ext
{ int field1;
double field2;
char metadata[256]; }
} ORIG, *pORIG;
int main(int argc, char ** argv)
{
pORIG * org = malloc(sizeof(ORIG));
org->field1 = 10;
org->field2 = 11.11;
strcpy(org->ext->metadata, "This is a test");
printf("%d %lf %s\n",org->ext->field1, org->ext->field2, org->ext->metadata);
}
Then there's nothing to copy and you can access it either way...
(BTW... I haven't done this one in a while, someone should probably check my syntax)