473,387 Members | 1,512 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

difference between structure & union

1
what is the difference between structure & union
Mar 19 '08 #1
6 4087
satch
23
what is the difference between structure & union
A quick google search told me that you've asked the same question on another forum and have been answered.
If you are still not clear about it then read the following :
http://bobobobo.wordpress.com/2008/01/25/c-difference-between-struct-and-union/
Mar 19 '08 #2
every data member in union uses the same memory location,thats why it is better option to save the memory.but we can use only one data member at a time.
Mar 19 '08 #3
every data member in union uses the same memory location,thats why it is better option to save the memory.but we can use only one data member at a time.
I guess most of the confusion comes when designers include unions as fields along with base types and structs in a new type definition. There seems to be no concern for the storage, just the convenience of hiding the details.
Mar 21 '08 #4
satch
23
I guess most of the confusion comes when designers include unions as fields along with base types and structs in a new type definition. There seems to be no concern for the storage, just the convenience of hiding the details.
hey dwurmfeld,
could you please give an example of the scenario you've mentioned?
Mar 24 '08 #5
hey dwurmfeld,
could you please give an example of the scenario you've mentioned?
.


.
Mar 24 '08 #6
hey dwurmfeld,
could you please give an example of the scenario you've mentioned?
Sure, here is a piece of nonsense code, using a formal CoordinateClass would make more sense, but from the point of view of the argument, the union of structures doesn't much help "save space", indeed, if this were an embedded application, could you really trust the sizeof operator?


Expand|Select|Wrap|Line Numbers
  1. #include <cstdlib>
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6. int main(int argc, char *argv[])
  7. {
  8.     typedef struct {
  9.        double x;
  10.        double y;
  11.        int imaginary;
  12.    } TComplex;
  13.  
  14.    typedef struct {
  15.       double x;
  16.       double y;
  17.    } TwoD;   
  18.  
  19.    typedef struct {
  20.       TwoD base2D;
  21.       double z;
  22.    } ThreeD;
  23.  
  24.     union UniversalCoordinate{
  25.        TComplex CoordComplex;
  26.        TwoD  Coord2D;
  27.        ThreeD Coord3D;
  28.     };
  29.  
  30.     // use the union
  31.     UniversalCoordinate fred;
  32.     fred.CoordComplex.x = 0.0;
  33.     fred.CoordComplex.y = 3.14;
  34.     fred.CoordComplex.imaginary = -3;
  35.     fred.Coord2D.x = fred.CoordComplex.x;
  36.     fred.Coord2D.y = fred.CoordComplex.y;
  37.     fred.Coord3D.base2D.x = fred.CoordComplex.x;
  38.  
  39.     cout << "The Ycoordinate is: " << fred.Coord3D.base2D.y << endl;
  40.     system("PAUSE");
  41.  
  42.     return EXIT_SUCCESS;
  43. }
  44.  
Mar 24 '08 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Memduh Durmaz | last post by:
Hi, I'm using DB2 UDB 7.2. Also I'm doing some tests on SQL Server 2000 for some statements to use efectively. I didn't find any solution on Sql Server about WITH ... SELECT structure of DB2. ...
4
by: csudha | last post by:
Hi All, Can you give me the example code which explains difference between Structures and Union. As a newbie please do the needful. Thanks, csudha.
10
by: tapeesh | last post by:
I created a C file say struct.c with the following structure declarations in the same file struct A { union key { int i; float f; }k1;
8
by: Charles Law | last post by:
Can anyone suggest how I would marshal a variable length structure back from an API call. Specifically, I am looking at the WaitForDebugEvent function, which returns a DEBUG_EVENT structure. ...
5
by: pt | last post by:
Hi, i am wonderng what is faster according to accessing speed to read these data structure from the disk in c/c++ including alignment handling if we access it on little endian system 32 bits...
45
by: anto frank | last post by:
hi friends, is ther any difference in array in c and array in c++?
0
by: manish sahu | last post by:
difference between structure and union ,, can anyone help me?
6
by: Ravikiran | last post by:
Hi, I want know the differences between Unions and Structures in C programming. Thank you..
8
by: Andrew Smallshaw | last post by:
I'm working on a data structure that began life as a skip list derivative, but has evolved to the point that it now only has a passing resemblance to them. Each node of this structure has a few...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.