Hi Guru's,
I have some problem while allocating memory to a union inside structure. Below is the code i am using
ON SYSTEM1:
This works fine
ON SYSTEM2:
compiler complains saying "need structure or union type" while allocating MYSTRUCT1.
if i change, shreyas[0].UnionAttr.struct1 = (MYSTRUCT1 *) malloc (sizeof(MYSTRUCT1)
to shreyas[0].UnionAttr->struct1 = (MYSTRUCT1 *) malloc (sizeof(MYSTRUCT1)
This compiler on SYSTEM2 is happy. but second way does not look correct to me and compiler on system 1 complains about it.
Can someone pls help know which is the correct way to allocate memory?
If first one is correct then what should i look in for to avoid this error?
Could this be an issue with compiler on SYSTEM2?
NOTE: if i use second method on SYSTEM2 code segfaults during malloc.
Please help ASAP, i am stuck because of this for long time.
printf("Struct 1 elements a = %d b = %d\n", shreyas[0].UnionAttr.struct1->a, shreyas[0].UnionAttr.struct1->b);
printf("Struct 2 elements a = %d b = %d\n", shreyas[1].UnionAttr.struct2->a, shreyas[1].UnionAttr.struct2->b);
printf("Success\n");
return;
Bookmarks