Try the following:
int main()
{
Node n;
Node *p;
n.type = 0;
n.u.child[0] = (Node *)malloc(sizeof(Node));
if (n.u.child[0] == NULL)
{
return 1;
}
n.u.child[1] = (Node *)malloc(sizeof(Node));
if (n.u.child[1] == NULL)
{
free(n.u.child[0]);
return 1;
}
p = n.u.child[0];
p->type = 10;
(n.u.child[1])->type = 24;
free(n.u.child[0]);
free(n.u.child[1]);
return 0;
}
NOTE. Do not modify n.u.valueNode if you have already assigned its child [] pointers. You will overwrite one of the pointers and skip this memory, as well as crash, if after that you try to access the child [] array. Unions are complex - it’s best to avoid this kind of arrangement.
source
share