3

I'm looking for an scenario where using Union is a better option than Structure in C?

I'm not looking for the difference between the two. I'm aware of the Structure and Union concepts in C, and the difference.

And I looked the question Difference between a Structure and a Union in C, which is no way the possible duplicate.

Community
  • 1
  • 1
Denim Datta
  • 3,740
  • 3
  • 27
  • 53
  • possible duplicate of [Difference between a Structure and a Union in C](http://stackoverflow.com/questions/346536/difference-between-a-structure-and-a-union-in-c) – devnull Nov 17 '13 at 13:33
  • 2
    `union` and `struct` are distinct, and non-comparable. – UltraInstinct Nov 17 '13 at 13:34
  • @devnull : I looked that question, but I'm looking for a relevant example. I have a little knowledge about the difference between the two. – Denim Datta Nov 17 '13 at 13:36
  • Why is this downvoted? I never get it... – Devolus Nov 17 '13 at 13:40
  • neither did I, they misunderstood the question, and linked it with some different query – Denim Datta Nov 17 '13 at 13:41
  • I didn't dv, but generally asking a question like this reveals almost complete lack of understanding the problem; and there's no source code either. – Aki Suihkonen Nov 17 '13 at 13:42
  • @Denim, that is usual behaviour. ;) – Devolus Nov 17 '13 at 13:42
  • Downvotes are because basic research would have found the answer proved by @Thrustmaster – Bull Nov 17 '13 at 13:44
  • 1
    @AkiSuihkonen : what source code. I can't find a scenario where "Union is better option than Structure", what source code to put there? – Denim Datta Nov 17 '13 at 13:44
  • 1
    @B..., the question was not `better` as in "is it better to have a green or a black tie". The question was, when is it better to use a union over a struct as in "Is it better to use a hammer or a screwdriver to hit the nail." – Devolus Nov 17 '13 at 13:45
  • @ Devolus Leave out the "to hit the nail". then "Is it better to use a hammer or a screwdriver". The answer it is better to use a hammer when you need a hammer and screwdriver when you need a screwdriver. TO know that you need to know the differenc between a screwdriver and a hammer. – Bull Nov 17 '13 at 13:48
  • 1
    @Denim, exactly -- some people dv immediately questions, that do not fall to the format, where source code is possible or mandatory. – Aki Suihkonen Nov 17 '13 at 13:49
  • @B..., The poster was already menmtioning in the original question that he knows the difference and was just looking for an example when one or the other is used. So how should he present a code example if that is what he was looking for to better understand the usage? If he woul dhave a coding example, he wouldn't have needed to ask in the first place. When I first learned about unions I had the sem problem and it helps to understand it if you have a real world example, instead of a theoretical explanation of the difference. – Devolus Nov 17 '13 at 14:14
  • @Denim: I didn't downvote, but putting the bolded content that you have now, in your original post (and/or some way of proving you did your research before asking) would have prevented some downvotes. People generally cast downvote and move on. They dont return to question. – UltraInstinct Nov 17 '13 at 14:19
  • Well you may as well argue whether a – phoeagon Nov 18 '13 at 02:02

4 Answers4

4

Well, consider the situation where you would like to be able to change each byte of an integer. You could use a union of the integer, and, for example, an array of 4 characters.

union Example
{
   int x;
   char array[4];
};

That way, by modifying one of the characters, you would also modify a corresponding byte (union members share memory space!).

However, that does not mean unions are better than structs, they're very different and comparing the two doesn't really make sense. It's just an example of how unions can be suitable for doing certain things.

Eutherpy
  • 4,471
  • 7
  • 40
  • 64
2

A union is a type that enables you to store different data types in the same memory space (but not simultaneously). A typical use is a table designed to hold a mixture of types in some order that is neither regular nor known in advance. By using an array of unions, you can create an array of equal-sized units, each of which can hold a variety of data types. unions are set up in much the same way as structures.
Another place you might use a union is in a structure for which the stored information depends on one of the members. For example, suppose you have a structure representing an automobile. If the automobile is owned by the user, you want a structure member describing the owner. If the automobile is leased, you want the member to describe the leasing company. Then you can do something along the following lines:

struct owner {
    char socsecurity[12];
    ...
};

struct leasecompany {
    char name[40];
    char headquarters[40];
    ...
};

union data {
    struct owner owncar;
    struct leasecompany leasecar;
};

struct car_data {
    char make[15];
    int status; /* 0 = owned, 1 = leased */
    union data ownerinfo;
    ...
};

Suppose flits is a car_data structure. Then if flits.status were 0, the program could use flits.ownerinfo.owncar.socsecurity, and if flits.status were 1, the program could use flits.ownerinfo.leasecar.name.


This is all taken from the book C Primer Plus 5th Edition
haccks
  • 104,019
  • 25
  • 176
  • 264
  • @user2018675; It is not **plagiarism**. Currently I am reading this book for the same purpose (structure and unions) :P. – haccks Nov 17 '13 at 13:55
  • I believe that when you quote something without giving the deserved credit for it making it looks like you are the one who said it, **it is** plagiarism indeed. – user2018675 Nov 17 '13 at 13:57
  • But now that you have added the source, I don't see any problems with your answer. – user2018675 Nov 17 '13 at 13:57
  • @user2018675; You are right. After giving answer I was in search of the right format (that I added to my answer) to add the reference. That's why it is delayed to add the reference. – haccks Nov 17 '13 at 13:58
1

A union is usefull when you have a datastrcuture which can be interpreted in different ways, but always using the same memory.

A good example is i.E. a 32 bit value (DWORD). You can read it as 2*16 bit values, 1*32 bit value or 4*8 bit values, so it is usefull, if you need to adress these parts individually, to create a union. This way you don't have to work with bitmasks or such. You could even create the individual bits, or sets of bits and access them as individual variables, using a union.

Using it to preserve memory is IMO not really needed, because you could always cast to different structures.

Devolus
  • 21,661
  • 13
  • 66
  • 113
0

Union is used in cases, where one is required to read a blob of data in multiple ways, or read data in different format than it was written. This is something a struct can not handle, unless one considers casting a data to different structs.

Casting can be prohibited in some coding conventions (especially if cast through void ptr), since that makes static analysis difficult. Even then, the comparison would be between union and cast, not between union and struct.

Aki Suihkonen
  • 19,144
  • 1
  • 36
  • 57