This question already has an answer here:
If it's a struct
then it can be done
*p = {var1, var2..};
But seems this doesn't work with union
:
union Ptrlist
{
Ptrlist *next;
State *s;
};
Ptrlist *l;
l = allocate_space();
*l = {NULL};
Only to get:
expected expression before ‘{’ token
Assign one of the fields to NULL. Since it is a union all the fields will be NULL.
There is difference between initialization and assignment. Initialization is intelligent, while for the assignment you need to resolve proper address.
In C99, you can use a designated union initializer:
In C++, unions can have constructors.
In C89, you have to do it explicitly.
By the way, are you aware that in C unions, all the members share the same memory space?
output:
see this a example of union initialization. in your case i think there is some mistake in
you should write
I don't have a
State
class so I replaced it with an int.this is my code:
So in this way, n's value is initialized to 10