gpt4 book ai didi

c - 在 Unix v6 中显然没有定义任何实例的结构

转载 作者:太空狗 更新时间:2023-10-29 15:01:26 26 4
gpt4 key购买 nike

我正在阅读 Lion 的书的 Unix 版本 6 的代码。其中一个头文件(param.h,可以访问 here )定义了以下结构:

/*struct to access integers*/

/*single integer */
struct { int integ; };

/*in bytes*/
struct { char lobyte; char hibyte; };

这些结构似乎没有定义任何实例,也没有命名以便以后使用。有人知道它们有什么用吗?

谢谢

最佳答案

如果有人将整个文件包含在 union 声明中,这将允许他们访问不同的部分。

它会是这样的:


union{
#include <param.h>
} myparam;

myparam.integ = 0xDEAD;
assert(myparam.lobyte == 0xAD)
assert(myparam.hibyte == 0xDE)

(取决于体系结构的字节序...)

所以环顾四周,似乎在旧版本的 C 中,您不需要声明 union ;所有结构/union 成员只有一个命名空间,它只是转换成一个字节偏移量,您可以在任何变量上使用它。我能找到的最好的提及是在这里: http://docs.sun.com/source/806-3567/compat.html描述 ISO 之前的 Sun C:

Allows struct, union, and arithmetic types using member selection operators ('.', '->') to work on members of other struct(s) or unions.

关于c - 在 Unix v6 中显然没有定义任何实例的结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3594367/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com