联合体占用内存大小是根据其最大元素所需要的内存所决定的,这就意味着,有下面的例子..

#include <iostream>
using namespace std;

union test
{
    int k;
    struct{int x, int y,int z}a;
}b;

int main()
{
   b.a.x = 1;
   b.a.y = 2;
   b.a.z = 3;
   b.k = 0;

   cout<<b.a.x<<b.a.y<<b.a.z<<endl;
   return 0;
}

程序的输出结果是 023 ,而不是 123。这是因为,union是共用内存,后来的赋值0将前面的1覆盖了。

 

相关文章:

  • 2021-04-20
  • 2021-06-21
  • 2022-12-23
  • 2021-08-22
  • 2021-08-30
  • 2022-01-09
  • 2021-10-20
  • 2021-04-21
猜你喜欢
  • 2021-06-21
  • 2022-12-23
  • 2021-04-17
  • 2022-12-23
  • 2022-12-23
  • 2021-09-26
  • 2022-12-23
相关资源
相似解决方案