Afficher un message
Vieux 17/10/2007, 23h03   #5
Fred Phillips
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: What's the use of anonymous structs?

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.

For example, consider the following code:

struct { int a; int b; } foo()
{
struct { int a; int b; } c;
c.a=5;
c.b=8;
return c; /* BOOM */
}

main()
{
struct { int a; int b; } c;
c=foo(); /* BANG */
printf("%d %d\n", c.a, c.b);
}

This won't compile - gives an error about incompatible return types at
line BOOM, and incompatible assignment types at line BANG.

So what's the point of allowing anonymous structs if they can't be
passed around in functions or assigned between each other?

  Réponse avec citation
 
Page generated in 0,05792 seconds with 9 queries