IT Community - Software Programming, Web Development and Technical Support

Make out of Structure and Union

This is a discussion on Make out of Structure and Union within the C and C++ Programming forums, part of the Software Development category; Hi all, Can anyone explain me about make out of Structure and Union in C....


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 12-17-2007, 03:45 AM
velhari velhari is offline
D-Web Programmer
 
Join Date: Mar 2007
Location: Chennai
Posts: 67
velhari is on a distinguished road
Send a message via AIM to velhari
Default Make out of Structure and Union

Hi all,

Can anyone explain me about make out of Structure and Union in C.
__________________
Regards,
VELHARI
I am not totally useless. I can be used for a bad example
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 03-12-2008, 04:24 AM
poornima poornima is offline
D-Web Sr.Programmer
 
Join Date: Dec 2007
Posts: 189
poornima is on a distinguished road
Smile Re: Make out of Structure and Union

Hi,
Let me explain u about Structure and Union

Structure
=======
Structure is a collection of elements having different datatypes
For eg..
Struct a
{
int b[5]; //->Memory allocated(10)
char c;//->Memory allocated(1)
};

Structure ends with semicolon...
Here the declaration tells that
'a'->Structure Variable
b[5]->b is a structure element that can hold 5-elements of same datatype[int]..(i.e., it can store 5-Integer Element)
c->character element..
For b[5]->10-bytes are allocated,c->'1-byte is allocated.
Totally for structure variable 'a'->11-bytes are allocated..
For each element separate memory is allocated..

But in Union,
Union c
{
int b;
char c;
};

Here it takes the variable of largest datatype and assign that single memory for all union elements..
i.e.,For union variable 'c' it allocates the total of 2-bytes both for element 'b' and 'c'.(i.e.,Refer same Memory location ,No individual memory for each variable)
This is the main difference between Union and Structure.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 04-03-2008, 02:55 AM
seesamjagan seesamjagan is offline
D-Web Programmer
 
Join Date: Aug 2007
Location: Chennai
Posts: 66
seesamjagan is on a distinguished road
Send a message via AIM to seesamjagan Send a message via Yahoo to seesamjagan
Post Re: Make out of Structure and Union

hi velhari,

Structure is a collection of memory block, of different data type, in which it will have separate memory space for each data type.
Structure Definition
Quote:
struct OrderInfo{
int orderId;
double orderAmounr;
string custName[20];
}
where as Union is a memory block where it has only one common memory space in which we can store any data but only one data at at time. (since every data share one common memory space, we can store only on data at a time)

UNion Definition
Quote:
UNION Shoping{
struct OrderInfo oi;
struct DispatchInfo di;
}
__________________
SeeSamJagan
- Sky is not the "LIMIT", Death is not the END, There is still something beyond....
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
Union and Union All itbarota Database Support 2 03-03-2008 12:41 AM
UNION ALL faster than a UNION? vadivelanvaidyanathan Database Support 4 08-18-2007 01:04 AM
what is the UNION in c++? with Example sundarraja C and C++ Programming 1 07-20-2007 06:44 AM
ence between “C structure” and “C++ structure”. ? Arun C and C++ Programming 1 07-18-2007 07:30 AM
What's the difference between a structure and a union prasath C and C++ Programming 1 07-18-2007 04:14 AM


All times are GMT -7. The time now is 01:53 PM.


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

SEO by vBSEO 3.0.0