Struct that apparently defines no instances in Unix v6
I'm going through the code of Unix version 6 with the Lion's book. One of the header files (param.h, can be accessed here) defines the following structs:
/*struct to access integers*/
/*single integer */
struct { int integ; };
/*in bytes*/
struct { char lobyte; char hibyte; };
These structures don't seem to define any instance, nor are they named so they can be us开发者_开发知识库ed later. Does anybody know what is their use?
Thanks
If someone included the whole file in a union declaration, it would allow them to access the different parts.
It would be something like:
union{
#include <param.h>
} myparam;
myparam.integ = 0xDEAD;
assert(myparam.lobyte == 0xAD)
assert(myparam.hibyte == 0xDE)
(Depends on endianness of architecture...)
So having looked around a bit, it seems that in old versions of C, you wouldn't have needed to declare the union ; there was only one namespace for all struct/union members that just translated into a byte offset that you could use on any variable. The best mention of this I could find is here : http://docs.sun.com/source/806-3567/compat.html Describing pre-ISO Sun C:
Allows struct, union, and arithmetic types using member selection operators ('.', '->') to work on members of other struct(s) or unions.
Back in those days, the members of structures all shared the same namespace, not one namespace per structure. Consequently, each element of a structure had to have a unique name across all structures, or the same element had to appear with the same type at the same offset in every structure in which it appeared. Quite how that was used with these, I'm not sure, but I suspect you could do:
int x;
x.lobyte = 1;
x.hibyte = 2;
Or something analogous to that.
See also:
- http://www.cs.bell-labs.com/who/dmr/chist.html
- http://www.cs.bell-labs.com/who/dmr/primevalC.html
(Neither of those seems to answer this question, though.)
精彩评论