I have some problem while allocating memory to a union inside structure. Below is the code i am using
This works fine
compiler complains saying "need structure or union type" while allocating MYSTRUCT1.
if i change, shreyas.UnionAttr.struct1 = (MYSTRUCT1 *) malloc (sizeof(MYSTRUCT1)
to shreyas.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.UnionAttr.struct1->a, shreyas.UnionAttr.struct1->b);
printf("Struct 2 elements a = %d b = %d\n", shreyas.UnionAttr.struct2->a, shreyas.UnionAttr.struct2->b);