NEED HELP

T

tanvir 095438

What is the difference between structure and union?


Mahmudul hasan shovon(095411)
CEE, Islamic University of technology
 
B

Bill Reid

What is the difference between structure and union?

Mahmudul hasan shovon(095411)
CEE, Islamic University of technology

Honest to God truth, I barely know that one my own
self...when you find out, could you post the answer?
TIA.
 
R

Ram Prasad

What is the difference between structure and union?

Mahmudul hasan shovon(095411)
CEE, Islamic University of technology

Is that the university homework ?
Why dont you google that for yourself ?

Thanks
Ram



PS:
Note to Spammers: Go ahead , send me spam
(e-mail address removed)
http://pragatee.com
 
K

kathir cpp

Here is the simple example:

All the members in Union will share the same memory location.
We have a same set of members for MyStruct and MyUnion. Compiler will
allocate 16 bytes for the given structure and allocate just 4 bytes
for the union.

struct MyStruct
{
long lValue;
char ch;
char buf[4];
float f;
};

union MyUnion
{
long lValue;
char ch;
char buf[4];
float f;
};

int _tmain(int argc, _TCHAR* argv[])
{
int sz1 = sizeof(MyStruct); // will be 16 bytes
int sz2 = sizeof(MyUnion); // will be 4 bytes
}

An example in VC++ would be VARIANT class.

To view more details on sizeof, visit at http://www.kathircpp.com
 
N

Nick Keighley

Here is the simple example:

All the members in Union will share the same memory location.
We have a same set of members for MyStruct and MyUnion. Compiler will
allocate 16 bytes for the given structure and allocate just 4 bytes
for the union.

struct MyStruct
{
    long lValue;
    char ch;
    char buf[4];
    float f;

};

union MyUnion
{
    long lValue;
    char ch;
    char buf[4];
    float f;

};

int _tmain(int argc, _TCHAR* argv[])
{
    int sz1 = sizeof(MyStruct); // will be 16 bytes
    int sz2 = sizeof(MyUnion); // will be 4 bytes

}

the actual sizes would be dependent on the implementaion, but you have
the right general idea.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Help 1
Printf & scanf 4
Turbo C 1
String in Turbo C 2
Series programme in Turbo C 0
Turbo C 0
C 2
Hello 3

Members online

No members online now.

Forum statistics

Threads
473,951
Messages
2,570,113
Members
46,699
Latest member
Abigail89S

Latest Threads

Top