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..

Recommended Answers

All 3 Replies

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.

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 :)

commented: good one +1

Structures are used when you want to use every member of the structure at the same time
while in union only one of the member can be used

Union is very helpful in many cases where there are switch cases and we have to use only one of the members in our list

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.