Advertisement

Unions

  • Mikael Olsson
Chapter

Abstract

The union type is identical to the struct type, except that all fields share the same memory position. Therefore, the size of a union is the size of the largest field it contains. In the following code, this is the integer field, which is four bytes.

union mix {
  char c;  /* 1 byte */
  short s; /* 2 bytes */
  int i;   /* 4 bytes */
};

Copyright information

© Mikael Olsson 2019

Authors and Affiliations

  • Mikael Olsson
    • 1
  1. 1.Hammarland, Länsi-SuomiFinland

Personalised recommendations