C Data Types - Unions

Unions

Union types are special structures which allow access to the same memory using different type descriptions; one could, for example, describe a union of data types which would allow reading the same data as an integer, a float or a user declared type:

union { int i; float f; struct { unsigned int u; double d; } s; } u;

In the above example the total size of u is the size of u.s (which is the sum of the sizes of u.s.u and u.s.d), since s is larger than both i and f. When assigning something to u.i, some parts of u.f may be preserved if u.i is smaller than u.f.

Reading from a union member is not the same as casting since the value of the member is not converted, but merely read.

Read more about this topic:  C Data Types

Famous quotes containing the word unions:

    When Hitler attacked the Jews ... I was not a Jew, therefore, I was not concerned. And when Hitler attacked the Catholics, I was not a Catholic, and therefore, I was not concerned. And when Hitler attacked the unions and the industrialists, I was not a member of the unions and I was not concerned. Then, Hitler attacked me and the Protestant church—and there was nobody left to be concerned.
    Martin Niemller (1892–1984)

    The newly-formed clothing unions are ready to welcome her; but woman shrinks back from organization, Heaven knows why! It is perhaps because in organization one find the truest freedom, and woman has been a slave too long to know what freedom means.
    Katharine Pearson Woods (1853–1923)