When to use a union and when to use a structure

后端 未结 4 2000
长情又很酷
长情又很酷 2021-01-01 03:24

I know the differences between union and structure. But from a design and coding perspective what are the various use cases of using a union instead of a structure? One is a

4条回答
  •  星月不相逢
    2021-01-01 04:24

    Ins some cases we may have to use only one variable at a time but the different results will have to be stored with different names. In such cases union will help by allocating same space for each variable at one location with the maximum variable size.

    i.e if we use int and char then union will allocate space for char which has the bigger size and the int too 'll be stored in the same space overriding the last one.

提交回复
热议问题