What is the difference between structure and union in C?

Differences between structure and union in C are presented in the following table. Structure and union are different in some ways yet they are conceptually same and have following similarities too:

Both are container data types and can contain objects of any type, including other structures and unions or arrays as their members.

Both structures and unions support only assignment = and sizeof operators.

In particular, structures and unions cannot appear as operands of the equality ==, inequality !=, or typecast operators. The two structures or unions in the assignment must have the same members and member types.

Both structure and union can be passed by value to functions and returned by value by functions. The argument must have the same type as the function parameter. A structure or union is passed by value just like a scalar variable; that is, the entire structure or union is copied into the corresponding parameter.

Difference between structure and union in C
structure union

Keyword struct defines a structure.

Keyword union defines a union.

Example structure declaration:

struct s_tag 
{
  int ival;
  float fval;
  char *cptr;
}s;

Example union declaration:

union u_tag 
{
  int ival;
  float fval;
  char *cptr;
}u;

Within a structure all members gets memory allocated and members have addresses that increase as the declarators are read left-to-right. That is, the members of a structure all begin at different offsets from the base of the structure. The offset of a particular member corresponds to the order of its declaration; the first member is at offset 0. The total size of a structure is the sum of the size of all the members or more because of appropriate alignment.

For a union compiler allocates the memory for the largest of all members and in a union all members have offset zero from the base, the container is big enough to hold the WIDEST member, and the alignment is appropriate for all of the types in the union.

When the storage space allocated to the union contains a smaller member, the extra space between the end of the smaller member and the end of the allocated memory remains unaltered.

Within a structure all members gets memory allocated; therefore any member can be retrieved at any time.

While retrieving data from a union the type that is being retrieved must be the type most recently stored. It is the programmer's responsibility to keep track of which type is currently stored in a union; the results are implementation-dependent if something is stored as one type and extracted as another.

One or more members of a structure can be initialized at once.

A union may only be initialized with a value of the type of its first member; thus union u described above (during example declaration) can only be initialized with an integer value.

Hope you have enjoyed reading the differences between struct and union in C. Please do write us if you have any suggestion/comment or come across any error on this page. Thanks for reading!



Share this page on WhatsApp

Get Free Tutorials by Email

About the Author

is the founder and main contributor for cs-fundamentals.com. He is a software professional (post graduated from BITS-Pilani) and loves writing technical articles on programming and data structures.