显然在Unix v6中没有定义实例的Struct

the*_*ole 5 c unix kernel

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

/*struct to access integers*/

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

/*in bytes*/
struct { char lobyte; char hibyte; };
Run Code Online (Sandbox Code Playgroud)

这些结构似乎没有定义任何实例,它们也没有命名,因此可以在以后使用.有谁知道它们的用途是什么?

谢谢

rjw*_*rjw 5

如果有人将整个文件包含在联合声明中,则允许他们访问不同的部分.

它会是这样的:


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

  myparam.integ = 0xDEAD;
  assert(myparam.lobyte == 0xAD)
  assert(myparam.hibyte == 0xDE)
Run Code Online (Sandbox Code Playgroud)

(取决于建筑的字节顺序......)

所以看了一下,似乎在旧版本的C中,你不需要声明联合; 所有struct/union成员只有一个名称空间,只是转换为可以在任何变量上使用的字节偏移量.我能找到的最好的一点就是这里:http: //docs.sun.com/source/806-3567/compat.html 描述pre-ISO Sun C:

允许使用成员选择运算符('.',' - >')的struct,union和arithmetic类型处理其他结构或联合的成员.

  • 我认为你是对的,在C的旧版本中,你不需要声明联盟; 所有struct/union成员只有一个名称空间,只是转换为可以在任何变量上使用的字节偏移量.我可以在这里找到最好的提及:http://docs.sun.com/source/806-3567/compat.html描述pre-ISO Sun C:<blockquote>允许使用成员的struct,union和arithmetic类型选择运算符('.',' - >')用于处理其他结构或联合的成员.</ BLOCKQUOTE> (2认同)