4 ms·
But "XX XX;" (the example was given in the article) is valid, and the parser needs to know the difference. The article says that this code is "evil", but I dis
by roel_v 16y ago
But "XX XX;" (the example was given in the article) is valid, and the parser needs to know the difference.
The article says that this code is "evil", but I disagree. There is a valid pattern that I use quite often (I described it in an article for ACCU a few years ago, too). It's an example from C++ but I think I could apply it to C as well. Consider (with a modification of the example I used in that article):
struct PirateShip {
struct Cannons {
char* cannon1;
char* cannon2;
} Cannons;
}
Now you can use
PirateShip s;
printf("Name of the first cannon: %s\n", s.Cannons.cannon1);
which is very readable and idiomatic (once you get used to it ;) )
More abstract, it provides an idiomatic way to group collections of objects/structs at the source code/syntactic level.
(one could name the struct and the member differently, I realize; the benefit of using the same name is when you define constant members in the inner struct, that way you can reference constants and properties with the same name and you never have to think about which one to use.)
(also again I use this from C++, there may be things that are different in C, but afaik this part is the same).
- eliben 16y agoI'm not sure how you use "XX XX" in that code sample. Do you refer to the "struct Foo {...} Foo;" idiom? That's quite different, though :)