C language_Union union introduced

Introduction of the concept of consortium and community

01——Introduction of the concept of consortium and community

Sometimes the same memory space stores different types, and variables of different types share a space.

  1. Structural elements have their own separate space, and common body elements share space. The size of the space is determined by the largest type.

  1. Define a union:

  1. Define a union variable:

  1. Calculate the length of the union:

Unions/unions are like structures but with differences:

02——Pay attention to the data coverage problem of the union

Struct elements do not affect each other, and assignment to a union will cause overwriting.

03——Unit development case

Example question expansion:

Guess you like

Origin blog.csdn.net/weixin_54859557/article/details/126085933