Richard Heathfield <rjh@see.sig.invalid> writes:
> Fred Phillips said:
>> Title says ["What's the use of anonymous structs?"]
>
> As far as I can discern, C doesn't provide such a feature. So the answer is
> "none, as far as C is concerned". If your implementation provides such a
> feature as an extension, consult its documentation to find out why.
Maybe. It depends on what the OP is really asking about.
You can declare something like this:
struct {
int x;
int y;
} obj;
The struct object "obj" is clearly not anonymous (its name is "obj"),
but the struct type has no name.
The advantage of this, I suppose, is that if you're only going to have
one object of the type, a name for the type would be superfluous. It
might be a decent way to provide a logical grouping for a set of
objects (almost like a C++ namespace, but far more restrictive).
But if C required all struct types to have a tag ("struct foo { ... }"),
it wouldn't have caused any real problems. In the few cases where the
tag name is irrelevant, you could just make up something arbitrary.
> As a rule, "What's the use of..." questions are pretty pointless. If you
> can't think of a use for something, don't use it.
It depends. If I'm learning a new language, and I run across a
feature for which I can't think of a use, learning how it's actually
used can be very instructive.
--
Keith Thompson (The_Other_Keith)
kst-u@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"