answersLogoWhite

0


Best Answer

Subudhi Pamisetty

With a union, you're only supposed to use one of the elements, because they're all stored at the same spot. This makes it useful when you want to store something that could be one of several types. A struct, on the other hand, has a separate memory location for each of its elements and they all can be used at once.

union foo {

int a; // can't use both a and b at once

char b;

} foo;

struct bar {

int a; // can use both a and b simultaneously

char b;

} bar;

union foo x;

x.a = 3; // OK

x.b = 'c'; // NO! this affects the value of x.a!

struct bar y;

y.a = 3; // OK

y.b = 'c'; // OK

edit: If you're wondering what setting x.b to 'c' changes the value of x.a to, technically speaking it's undefined. On most modern machines a char is 1 byte and an int is 4 bytes, so giving x.b the value 'c' also gives the first byte of x.a that same value: union foo x;

x.a = 3;

x.b = 'c';

printf("%i, %i\n", x.a, x.b);

prints 99, 99

Why are the two values the same? Because the last 3 bytes of the int 3 are all zero, so it's also read as 99. If we put in a larger number for x.a, you'll see that this is not always the case: union foo x;

x.a = 387439;

x.b = 'c';

printf("%i, %i\n", x.a, x.b);

prints 387427, 99

To get a closer look at the actual memory values, let's set and print out the values in hex: union foo x;

x.a = 0xDEADBEEF;

x.b = 0x22;

printf("%x, %x\n", x.a, x.b);

prints deadbe22, 22

You can clearly see where the 0x22 overwrote the 0xEF.

BUT

In C, the order of bytes in an int are not defined. This program overwrote the 0xEF with 0x22 on my Mac, but there are other platforms where it would overwrite the 0xDE instead because the order of the bytes that make up the int were reversed. Therefore, when writing a program, you should never rely on the behavior of overwriting specific data in a union because it's not portable.

User Avatar

Wiki User

13y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

10y ago

When you have two objects and they are not used together, you can use union to save memory.

Example:

In a hotel, a room is shared by two members, but at any cost of time they won't overlap or stay together. Just assuming a person in Night shift work and another one in Day shift. We gave them one room to save rooms.

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

Type your answer here... gfnfgnfgnfgxn

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Where would you use union in C language?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

What is the use of loader in c language?

There is no part called 'loader' in C language.


How you use stack in c plus plus classes?

It would be easier to manipulate the stack in assembly language rather than C++.


What computer language java or C plus plus should you use to design an app for the ipod or should i use a software?

You would use neither Java nor C++, you would use Objective-C, in conjunction with the Apple iPod API (iPod Library Access).


What programming language does the PSP use?

c++


What is use of l in c language?

Bitwise OR.

Related questions

Where would you use union in C?

Type your answer here... gfnfgnfgnfgxn


Why you are use to c language?

I do use am a programmer, because C-language.


What is structure and union?

Structure is a query that is used in language c++


What is the use of loader in c language?

There is no part called 'loader' in C language.


How can i make a computer program?

If you want to create well....... pretty much anything you want then I would learn C++ which is called a programming language. I would use the C++ editor Dev-C++ (http://www.bloodshed.net/devcpp.html) or Microsoft Visual Studio. You can also learn how to program as a beginner using DarkBASIC (darkbasic.com) - (darkbasicpro.com). But I would learn a BASIC language then move on to a C language (C , C# , C++). But its your choice. You can also use Game Maker which is EXTREMELY simple. Hope it helped!


How can i make a program?

If you want to create well....... pretty much anything you want then I would learn C++ which is called a programming language. I would use the C++ editor Dev-C++ (http://www.bloodshed.net/devcpp.html) or Microsoft Visual Studio. You can also learn how to program as a beginner using DarkBASIC (darkbasic.com) - (darkbasicpro.com). But I would learn a BASIC language then move on to a C language (C , C# , C++). But its your choice. You can also use Game Maker which is EXTREMELY simple. Hope it helped!


How do you write a programme in c language using arrays to copy diagonal elements of a 2-d array into a 1-d array?

TO use a c language first step is to know about the c language and the steps to use the c progrmming language with the help of any elders or with the teachers. TO use the arrays you have to get th eknowledge of "c" language


How you use stack in c plus plus classes?

It would be easier to manipulate the stack in assembly language rather than C++.


What computer language java or C plus plus should you use to design an app for the ipod or should i use a software?

You would use neither Java nor C++, you would use Objective-C, in conjunction with the Apple iPod API (iPod Library Access).


Which compiler you must use to run C language?

To run C language?! Your question makes no sense.


Why c plus plus is important?

It is a widely used computer language; one of the reasons is that it is a stricter way of compiling C code if you just use the procedural aspect of the language. Along with that, it has the object oriented model as well. Some would call it a 'better C' language.


Explain use of graphics in C language?

C has nothing to do with graphics.