这是什么语法:工会{}? [英] What is this syntax: union{}?

查看:131
本文介绍了这是什么语法:工会{}?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

void display_binary_float(unsigned int ui) {
  union {
    unsigned int ui;
    float f;
  } uif2;
  uif2.ui = ui;
  printf("binary: %08X float: %g\n", uif2.ui, uif2.f);
}

1)什么是工会?没有人工输入它,出于某种原因。找不到对谷歌的文档。

1) What is union? There was no manual entry for it, for some reason. Couldn't find the doc on google.

2)为什么 uif2 在功能结合的结束?难道不应该是这样的工会uif2 {} 或这是一个C的东西?

2) Why is uif2 at the end of the function union? Shouldn't it be something like union uif2 {} or is this a C thing?

推荐答案

一个工会,您可以把内存的不同变量/变量类型的块。每个变量共享相同的存储器,以及由联合使用的内存的总量是由最大的构件的用量

A union allows you to treat a block of memory as different variables/variable types. Each variable shares the same memory, and the total amount of memory used by the union is the amount used by the largest member.

访问工会成员也正是做,你会访问结构成员。但是,当结构包含成员,每个包含自己的记忆,在工会成员共享相同的内存,因此一个或多个成员可能无效。

Accessing union members is done exactly as you would access structure members. But while structures contain members that each contain their own memory, members in a union share the same memory and so one or more member may invalid.

显然,你不能在多个值存储在一个工会。但是,为要在其中在一种情况下的一个数据类型,并在另一种情况下另一种数据类型的情况下,将其提供给存储这些不同的数据类型中的一个方便的方法。

Obviously, you can't store multiple values in a union. But, for cases where you want one data type in one case and another data type in another case, it provides a convenient way to store one of those different data types.

这篇关于这是什么语法:工会{}?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆