What is a union?

前端 未结 5 2016
轮回少年
轮回少年 2021-02-08 09:58

Recently I am working on windows and I found that lot of data structures are defined as struct with union as member variables. Example of this would be

5条回答
  •  生来不讨喜
    2021-02-08 10:55

    union means you can have one of its members as its possible value. In the following example, you see the values for each one of them. But this union can be a member of some other structure, where it is enough to specify only one value- either float or integer not both. Hope this helps.

      union {
              float u_f;
              int u_i;
      }var;
    
      var.u_f = 23.5;
      printf("value is %f\n", var.u_f);
      var.u_i = 5;
      printf("value is %d\n", var.u_i)
    

提交回复
热议问题