Q:

How are we able to assign two values in SDL_Event(a union)?

I was reading lazyfoo's SDL2.0 tutorials and on the third tutorial it is shown how to use SDL_Event, I went for exploring more of SDL_Event on SDL_Event's tutorial page and found its of Union type but down the page on the Placing events section they are using SDL_Event (a union) to assign two values one of type and other to user, my doubt is how is it possible to assign two values to an Union without an bug or illegal assignment?
I did went to check union's definition again here but it also said same that you can't assign two values to a union at the same time.
P.S.
the code at above sdl link was :
SDL_Event user_event;

user_event.type=SDL_USEREVENT;
user_event.user.code=2;
user_event.user.data1=NULL;
user_event.user.data2=NULL;
SDL_PushEvent(&user_event);

A:

Unions use one piece of memory, but access to that memory can be done through any of the "unioned" types.  The point is to never assign values (for example) to the user member and the mouse member.  When you go to read the values back, the result is undefined.
In other words, this is fine:
user_event.user.code=2;
user_event.user.data1=NULL;

But this is not:
user_event.user.code=2;
// BAD!  'key' is from a different type in the union!
user_event.key.state=SDL_PRESSED;

Now, the type member in particular is special.  SDL has that same variable (data type and name) as the first member in every one of the possible event types.  It also has the same variable as the first member of the whole union.  This way, the memory always has this type variable first and can be reliably accessed no matter which union member it is accessed through.  This is a special case in the C specification of unions, where "If a union contains several structures that share a common initial sequence, and if the union object currently contains one of these structures, it is permitted to inspect the common initial part of any of them".  This is in Section 3.3.2.3 in my draft copy of the C89 spec.

