O'Reilly logo

Just Enough C/C++ Programming by Guy W. Lecky-Thompson

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

The struct and union Keywords

Superficially, structs and unions work in the same way—they provide the definition of a single entity that can contain different kinds of data, and you can use that definition to create objects that are individual instances of that data.

However, there is a fundamental difference between the two in that a struct is fully populated, and a union provides a mechanism to selectively populate the data contained in the object. Thus, the definition will be different from the final implementation.

In terms of storage, an instance of a struct occupies a space in memory that is as big as the size of each individual data type inside it. So if the struct contains six integers, the memory space will be six integers wide. On the ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required