Structures And Unions
Please someone help me regarding the differencec b/w Structures and Unions.I know the differnce in the memory management but what is itz advantage .Also why are unions specifically used in mouse programming and similar stuff..
alvalany
Junior Poster in Training
61 posts since Jul 2009
Reputation Points: 8
Solved Threads: 2
unions are used whenever the same data can be represented in two or more ways, or by two or more data types. Normally I think unions are place inside structures, something like a VARIANT structure . This structure has two members, an integer that tells which of the union members are used, and then the union of 20 or so data types.
>>.Also why are unions specifically used in mouse programming
AFAIK unions are not used for that purpose because the program needs to know both the x and y coordinates of the mouse. A union would not provide that information.
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
Normally I think unions are place inside structures
They can also be very helpful outside structures, for example in this snippet I made some time ago. [link]
I used it in the main scope as a structure inside of a union instead of the other way round. It helps when you want to assign values quickly. In fact, in the Pixel struct, i use a struct inside a union inside a struct :)
William Hemsworth
Posting Virtuoso
1,591 posts since Mar 2008
Reputation Points: 1,429
Solved Threads: 129