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

标签 c unix kernel

我正在阅读 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/

相关文章:

linux-kernel - printk 不适用于内核调试

c - 如何将宏调用函数转换为内核函数?

python - PyCharm 终端错误

c - C UNIX 中的隐式函数声明

python - 使用 Popen 减少控制

linux-kernel - 在 kretprobe 处理程序中获取函数的返回值

c++ - 多线程编程如何保证执行顺序?

c++ - 一个字节中的两个值

c - 结构的两个 malloc 定义之间的区别

c - 重新分配原始数组后,数组元素会发生什么?