IT Community - Software Programming, Web Development and Technical Support

What's the difference between a structure and a union

This is a discussion on What's the difference between a structure and a union within the C and C++ Programming forums, part of the Software Development category; Hi, can anyone tell,What's the difference between a structure and a union? Thanks, Prasath.K...


Go Back   IT Community - Software Programming, Web Development and Technical Support > Software Development > C and C++ Programming

Register FAQ Members List Calendar Mark Forums Read
  #1 (permalink)  
Old 07-18-2007, 02:54 AM
prasath prasath is offline
D-Web Sr.Programmer
 
Join Date: Jul 2007
Location: Chennai
Posts: 173
prasath is on a distinguished road
Smile What's the difference between a structure and a union

Hi,
can anyone tell,What's the difference between a structure and a union?


Thanks,
Prasath.K
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-18-2007, 05:14 AM
vigneshgets vigneshgets is offline
D-Web Genius
 
Join Date: Mar 2007
Posts: 904
vigneshgets is on a distinguished road
Wink Re: What's the difference between a structure and a union

Hi prasath i guess this reply will fullfill ur query "While structure enables us treat a number of different variables stored at different in memory , a union enables us to treat the same space in memory as a number of different variables. That is a Union offers a way for a section of memory to be treated as a variable of one type on one occasion and as a different variable of a different type on another occasion.

Union allocates the memory equal to the maximum memory required by the member of the union but structure allocates the memory equal to the total memory required by the members.

Union allocates the memory equal to the maximum memory required by the member of the union but structure allocates the memory equal to the total memory required by the members.

Example:

union exforsys_t {
char c;
int i;
float f;
} exforsys;


Defines three elements:

exforsys.c
exforsys.i
exforsys.f

Each one with a different data type. Since all of them are referring to the same location in memory, the modification of one of the elements will affect the value of all of them. We cannot store different values in them independent from each other."
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Make out of Structure and Union velhari C and C++ Programming 2 04-03-2008 03:55 AM
UNION ALL faster than a UNION? vadivelanvaidyanathan Database Support 4 08-18-2007 02:04 AM
Difference between "C structure" and "C++ structure". Sabari C and C++ Programming 2 08-09-2007 08:41 AM
What is the difference between UNION and UNIONALL in SQL Server? bluesky Database Support 1 07-25-2007 05:53 AM
Difference between "C structure" and "C++ structure". vigneshgets C and C++ Programming 1 07-17-2007 01:11 AM


All times are GMT -7. The time now is 08:24 AM.


Copyright ©2004 - 2007, DiscussWeb. All Rights Reserved.

SEO by vBSEO 3.0.0