Best Answer

A union in C++ is similar to a C-style union in that the members of the union are all mapped to the same memory address. In other words, if you were to unify a long with a char, the char would be mapped to the first byte of the long. However, unlike C unions, C++ unions can be derived from other unions, structs or classes, as well as act as base classes for derived unions, structs and classes.

User Avatar

Wiki User

10y ago
This answer is:
User Avatar
Study guides
See all Study Guides
Create a Study Guide
12 Reviews
More answers
User Avatar

Wiki User

11y ago

The basic advantage is that union will use the memory space of the datatype which has the highest memory....

hence memory consumption will be less...

But when u use structure the total memory will be the sum of the memory of all datatypes.. ie.(higher memory allocation)

Disadvantage is that..... when you use the union.... only the last entered variable can be directly accessed as only the last added data to the union will exist in the memory...

This answer is:
User Avatar

User Avatar

Wiki User

6y ago

We use a union whenever we need to select between two or more representations where only one representation is in use (active) at any given time. All members of a union share the same memory address, thus a union provides a convenient means of reducing memory consumption when we require alternate representations. The length of a union is equal to the longest member (in bytes) but, otherwise, all members behave according to their respective types.

The main disadvantage is that there's nothing to prevent a programmer from writing to one member and subsequently reading another member. Although this unavoidable shortcoming is often used to implement a cast between member types that share a common representation (a 32-bit integer and a 4-byte signed char array, for instance), it's tempting to use this technique to forcefully cast between types that do not share a common representation (an int and a pointer to int would be an unsafe cast, for instance). In short, use unions for the purpose intended. If you have to shortcut the built-in casts using union members, you should seriously reconsider your design choices.

For members that do not share a common representation, unions must be used in conjunction with a type field to determine which member is currently active. In some cases, the type field can be dual purpose and therefore does not consume any additional memory (after all, the intent is to save memory), however where there is a cost, it needn't be expensive. Most unions have relatively few members such that an enumerated char type generally suffices for a type field. Typically, the union and its type field will be members of the same struct.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What are advantages and disadvantages of union in c language?
Write your answer...
Still have questions?
magnify glass
Continue Learning about Engineering

How memory space is occupied by union variables in C programming language?

A union is an aggregate of members that share the same memory address. The size of a union is determined by the largest member.

Why c language has name c why not a?

C-language was derived from B-language.

What are the advantages and disadvantages of C language?

Advantages of C languageSpeed of the resulting application. C source code can be optimized much more than higher level languages because the language set is relatively small and very efficient.That leads to a second advantage that C has which is its application in Firmware programming (hardware). That is due to its ability to use/work with assembly and communicate directly with controllers, processors and other devices.C Programming language is very easier to learn. The main advantages of C language is that there is not much vocabulary to learn, and that the programmer can arrange for the program is very fast.Disadvantages of C LanguageC does not have OOPS feature that's why C++ is developed. If you know any other modern programming language then you already know its disadvantages.There is no runtime checking in C language.There is no strict type checking (for example: we can pass an integer value for the floating data type).C doesn't have the concept of namespace.C doesn't have the concept of constructors and destructors.

What is C language what does it do?

C Language is First Step of Programming Language, Help for C Language you are show the correct answer

What are the advantages of structures of C language?

we can create a program which is useful for others and can resolve the problem in less time

Related questions

What is the Advantages and disadvantages of server side includes?


What are the advantages and disadvantages of using a DVD storage device?


What arethe advantages and disadvantages of aflowchart IN c language?

Nope, as flowcharts aren't part of C-language. Nonetheless, you can implement algorithms specified by flowcharts in C, but that might lead to unstructured code (also known as 'spaghetti-code'), which is outdated by forty years... Try using stuctograms instead.

Advantages of c language in cryptography?

You can use OpenSSL.

What is structure and union?

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

What are the disadvantages of using c?

There is basically no disadvantage of using a language like C. It depends on the programmer how he uses his skills.

What are the advantages and disadvantages of writing operating system in high level language such as c?

Nintendo is in Japanese like NREIALKEN GFRTAO and Carue is in Espan'ol like YURKO WILF as high level of Nintendo and Carue of something.

What are the advantages and disadvantages of an air conditioner?

it gets hot turn the a/c on it might not work with a dirty filter

What are the advantages of turbo C programming language?


A compiler that translates a high level-language into another high-level language is called a source-to-source translator. What advantages are there to using C as a target language for a compiler?

C would be a good language if you wanted to then go on to improve the efficiency of the code perhaps by editing the translated code. I wouldn't call C a high language :)

How memory space is occupied by union variables in C programming language?

A union is an aggregate of members that share the same memory address. The size of a union is determined by the largest member.

What is similarity among structure union and enum in c language?

all the three are user defined datatypes