|
|
|
|
||||||
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 |
|
Messages: n/a
Hébergeur: |
Title says it all...
|
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
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. -- Richard Heathfield <http://www.cpax.org.uk> Email: -http://www. +rjh@ Google users: <http://www.cpax.org.uk/prg/writings/googly.php> "Usenet is a strange place" - dmr 29 July 1999 |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
On Oct 17, 4:12 pm, Fred Phillips <n...@spam.invalid> wrote:
> Title says it all... In the future, please include your question in the body of the post. > What's the use of anonymous structs? There are (fairly rare) occasions where you want to include a group of fields, but care little about the grouping provided by the structure, and, for whatever reason, you find that the extra level of reference is burdensome or doesn't contribute to clarity. So you can do something like: struct date {int year; int month; int day;}; struct foo {int a; struct date; int b;}; int func(struct foo *s) { return s->month; /* note lack of intermediate qualification */ } This is somewhat more common with a union, where you have several items whose storage you want to overlap, but other than that they're not really members of a group, and the union name is just clutter. For example: struct bar {int a; union {int aa; double bb; long cc;}; int c;}; Which then allows: struct bar sb; .... sb.bb = 1.0; As I said, fairly rare. |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
On Oct 17, 4:45 pm, Richard Heathfield <r...@see.sig.invalid> wrote:
> As far as I can discern, C doesn't provide such a feature. I was thinking that had gotten included in C99... Nevermind... |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
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? |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
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" |
|
|
|
#7 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#8 |
|
Messages: n/a
Hébergeur: |
On Oct 17, 5:34 pm, Eric Sosman <esos...@ieee-dot-org.invalid> wrote:
> 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 ... Doesn't this contradict 6.7.2.3p4? Best regards, Yevgen |
|
![]() |
| Outils de la discussion | |
|
|