Fred Phillips wrote:
> On 17 Oct 2007 at 21:45, Richard Heathfield wrote:
>> 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.
>>
>> 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.
>
> Sorry, I guess I wasn't clear. I mean things like
> struct { int a; int b; } c;
> which is certainly valid C. However, this anonymous struct doesn't seem
> to be compatible with any other anonymous struct of the same signature.
> [...]
It's compatible with an identically-declared struct in
a different module ("translation unit") but not with such
a struct in the same module. (Thus, we can have two struct
variables in one module that are incompatible with each
other, but are each compatible with a third struct in a
different module -- strange, but true.)
What good are they? Sometimes they're useful as single-
instance global variables, particularly in small programs.
void func1(int, double);
void func2(int, double);
void func3(int, double);
static struct {
char *name;
void (*func)(int, double);
} funcsToTest[] = {
{ "func1", func1 },
{ "func2", func2 },
{ "blue funk", func3 },
};
.... is a construct I've used in connection with timing tests
of alternative implementations of something or other. The
program doesn't need to create additional instances, or even
to point at them: It just plucks information from this little
table of goodies and uses it.
--
Eric Sosman
esosman@ieee-dot-org.invalid