Structures And Unions

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jul 2009
Posts: 47
Reputation: itzAB is an unknown quantity at this point 
Solved Threads: 2
itzAB itzAB is offline Offline
Light Poster

Structures And Unions

 
0
  #1
Oct 6th, 2009
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..
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,578
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1486
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning
 
-7
  #2
Oct 6th, 2009
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.
Last edited by Ancient Dragon; Oct 6th, 2009 at 2:16 am.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 1,482
Reputation: William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of 
Solved Threads: 123
Sponsor
William Hemsworth William Hemsworth is offline Offline
Nearly a Posting Virtuoso
 
2
  #3
Oct 6th, 2009
Originally Posted by Ancient Dragon
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
Last edited by William Hemsworth; Oct 6th, 2009 at 6:58 am.
I need pageviews! most fun profile ever :)
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 27
Reputation: ankur_ is an unknown quantity at this point 
Solved Threads: 2
ankur_ ankur_ is offline Offline
Light Poster
 
0
  #4
Oct 27th, 2009
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
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC