Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I've inherited a bit of code and saw something new. They have a data structure definition out in a .h file, and in various local files they'll declare:

union globalStructOstuff localVar;

Is there any merit to this?

I could see some if the global was some placeholder and the local versions were going to be different eventually. But that's not the case here.

So I think all it's doing is causing new guys a couple of extra minutes searching for definitions. I'd axe it once I got the chance, but I wanted to see if I'm missing some greater purpose.

Edit, resolution
With your fine help, I managed to open my eyes and realize globalStructOstuff isn't a struct at all, it's a union which holds the struct of a similar name. And they didn't use typedef. So all that's happening here is declaring a local variable. It has a type of union. No alias to anything global. Sorry for the confusion, thanks for all your help.

share|improve this question
up vote 3 down vote accepted

That looks like a variable declaration.

In C declaring a variable of a struct or union requires the struct or union keyword (C++ removed this requirement).

Most people use a typedef to automatically include the keyword into the type name, as Blrfl did in his code example.

share|improve this answer
hmmm? But that's a struct not a.... AWWWW BLOODY hell. They ARE just declaring a variable which is a union. GlobalStructOstuff isn't a struct at all! Well this is simpler then I thought. Thanks dude. – Philip Oct 13 '11 at 13:20

Check the usage carefully before you remove it, because there are some applications that merit using a construct whose internal structure can vary. The fact that it's a union and not a struct says this might one of those situations. Look for some field that's common to all of the union's variants which is used to tell code that refers to it which arrangement to use in interpreting the contents.

The X Window System's client-side library uses this technique to allow event messages received from the server to be dropped directly into a union and easily accessed without casting or other nastiness:

typedef struct {
        int type;               /* of event */
        unsigned long serial;   /* # of last request processed by server */
        Bool send_event;        /* true if this came from a SendEvent request */
        Display *display;       /* Display the event was read from */
        Window window;          /* "event" window it is reported relative to */
        /* ...etc... */
} XButtonEvent;

typedef union _XEvent {
        int type;               /* must not be changed; first element */
        XAnyEvent xany;
        XKeyEvent xkey;
        XButtonEvent xbutton;
        XMotionEvent xmotion;
        /* ...etc... */
} XEvent;
share|improve this answer

Some IDE's have a panel or window that lists classes, structs, etc. and their members for quick reference. Possibly, declaring a union in this case serves no purpose other than to make the variable show up in such a list, or to change how or where it's listed by some documentation tool. Otherwise it seems pointless, but then people do strange things for reasons only they know sometimes.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.