Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the major difference between "union" and "struct" in C.? [duplicate]

Tags:

c++

c

struct

unions

Possible Duplicate:
Difference between a Structure and a Union in C

I could understand what a struct means. But, i am bit confused with the difference between union and struct. Union is like a share of memory. What exactly it means.?

like image 691
Vinay Avatar asked Oct 23 '10 08:10

Vinay


People also ask

What is the difference between a struct and a union in C?

Both structure and union are user-defined data types in C programming that hold multiple members of different data types. Structures are used when we need to store distinct values for all the members in a unique memory location while unions help manage memory efficiently.

How union is different from struct?

The structure allows initializing multiple variable members at once. Union allows initializing only one variable member at once. It is used to store different data type values. It is used for storing one at a time from different data type values.

Which is better to use union or struct?

If you want to use same memory location for two or more members, union is the best for that. Unions are similar to the structure. Union variables are created in same manner as structure variables. The keyword “union” is used to define unions in C language.

What is the difference between structure and union in C explain with example?

member definition; }structure variable declaration; Union in C is a special data type available in C that allows storing different data types in the same memory location. You can define a union with many members, but only one member can contain a value at any given time.


2 Answers

With a union, all members share the same memory. With a struct, they do not share memory, so a different space in memory is allocated to each member of the struct.

For example:

union foo {  int x;  int y; };  foo f; f.x = 10; printf("%d\n", f.y); 

Here, we assign the value of 10 to foo::x. Then we output the value of foo::y, which is also 10 since x and y share the same memory. Note that since all members of a union share the same memory, the compiler must allocate enough memory to fit the largest member of the union. So a union containing a char and a long would need enough space to fit the long.

But if we use a struct:

struct foo {  int x;  int y; };  foo f; f.x = 10; f.y = 20; printf("%d %d\n", f.x, f.y); 

We assign 10 to x and 20 to y, and then print them both out. We see that x is 10 and y is 20, because x and y do not share the same memory.

EDIT: Also take note of Gman's comment above. The example I provided with the union is for demonstration purposes only. In practice, you shouldn't write to one data member of a union, and then access another data member. Usually this will simply cause the compiler to interpret the bit pattern as another type, but you may get unexpected results since doing this is undefined behavior.

like image 64
Charles Salvia Avatar answered Sep 21 '22 15:09

Charles Salvia


I've used unions to convert bytes to and from other types. I find it easier than bit-shifting.

union intConverter {     int intValue;     struct {         byte hi;         byte lo;     } byteValue; }  intConverter cv; cv.intValue =1100; printf("%X %X\n", cv.byteValue.hi, cv.byteValue.lo); 

Where int is 16-bit (was used on a micro controller).

like image 40
Sam Avatar answered Sep 18 '22 15:09

Sam