25 ms·
In the old days it was possible to access members of structs inside unions without having to name the intermediate struct. For example the code in the sed
by binarycrusader 13y ago
In the old days it was possible to access members of
structs inside unions without having to name the
intermediate struct. For example the code in the sed
implementation uses rep->ad1 instead of rep->reptr1.ad1.
That’s no longer possible (I’m pretty sure this shortcut
was already out of fashion by the time K&R was published
in 1978, but I don’t have a copy to hand).
Wrong; it didn't just work in the "old days". The functionality described here is actually known as "anonymous struct" and is now a part of C11; but unfortunately not a part of C++11:
http://stackoverflow.com/questions/8622459/why-does-c11-not-support-anonymous-structs-while-c11-does http://stackoverflow.com/questions/8622459/why-does-c11-not-...
gcc and other compilers have also supported this functionality for a long time, although as a non-standard extension. This technique is also still widely used in systems-level programming and is hardly "ghastly" although I'll readily admit that there are better ways to accomplish what this particular case was doing.
- 0x09 13y agoThis is not an anonymous structure in the modern sense (if you look at the code they are in fact named), it's a side-effect of pre-ANSI C namespace rules. Any struct field could be used to calculate an offset from any other object. From http://cm.bell-labs.com/who/dmr/chist.html http://cm.bell-labs.com/who/dmr/chist.html : > Beguiled by the example of PL/I, early C did not tie structure pointers firmly to the structures they pointed to, and permitted programmers to write pointer->member almost without regard to the type of pointer; such an expression was taken uncritically as a reference to a region of memory designated by the pointer, while the member name specified only an offset and a type.
- binarycrusader 13y agoI was responding to the author's general claim that "In the old days it was possible to access members of structs inside unions without having to name the intermediate struct.". However, I stand corrected in two regards. The first is that as of gcc 4.6+, the sed example the author provided is no longer allowed even if the structs are unnamed. (I was testing with gcc 3.4.) I verified that gcc 3.4 would happily allow me to use the provided sed example as long as I removed the names from the structs. Second, the C11 standard specifically does not allow for the sed example provided because the structs in the unions contain duplicate members. I had seen reasoning that hinted at what you're saying in one of the comments posted on the author's blog entry. Regardless, thanks for encouraging me to dig deeper.