|
|
|
|
||||||
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#9 |
|
Messages: n/a
Hébergeur: |
Fred Phillips said:
> 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; Oh, I see. Yes, that's legal all right. But my answer remains as it was before: >> 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. <snip> > So what's the point of allowing anonymous structs if they can't be > passed around in functions or assigned between each other? Well, you can use 'em in typedefs: typedef struct { int a; int b; } typename; Now you can instantiate them via the synonym, and a tag would indeed be pretty pointless. But frankly, I prefer to separate the typedef from the struct definition, which of course brings us right back to the tag. -- 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 |
|
|
|
#10 |
|
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 ("translation unit") Doesn't it contradict 6.7.2.3p4: "Each declaration of a structure, union, or enumerated type which does not include a tag declares a distinct type"? Regards, Yevgen |
|
|
|
#11 |
|
Messages: n/a
Hébergeur: |
On Oct 18, 2:12 am, Fred Phillips <n...@spam.invalid> wrote:
> Title says it all... One common usage is structure inside structure/union. struct rect { struct { int x; int y } start; struct { int x; int y } end; }; |
|
|
|
#12 |
|
Messages: n/a
Hébergeur: |
ymuntyan@gmail.com wrote:
> On Oct 17, 5:34 pm, Eric Sosman <esos...@ieee-dot-org.invalid> wrote: >> Fred Phillips wrote: >>> [...] >>> 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? 6.7.2.3p4 says the types are distinct, but 6.2.7p1 says they are compatible anyhow. -- Eric Sosman esosman@ieee-dot-org.invalid |
|
|
|
#13 |
|
Messages: n/a
Hébergeur: |
On Wed, 17 Oct 2007 14:51:19 -0700,
robertwessel2@yahoo.com <robertwessel2@yahoo.com> wrote: > 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 */ > } Can you provide an example of a compilable program that does this? The above is certainly refused by gcc (in c89 and c99 mode, as well as in its default mode) I agree with gcc on this one, in that I don't believe the above is valid C. Are you maybe thinking of another language? Which compiler supports this? > struct bar {int a; union {int aa; double bb; long cc;}; int c;}; > > Which then allows: > > struct bar sb; > ... > sb.bb = 1.0; Ditto for this Martien -- | Martien Verbruggen | This matter is best disposed of from a great | height, over water. | |
|
|
|
#14 |
|
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?"] > 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. I wrote some simulation code recently that had a large module with many file scope variables containing various states of the simulation. I wanted to group several of them having to do with a particular aspect, for example, motor. Rather than defining motorSpeed, motorTorque, motorTemp, and motorCurrent, I choose to group them as static struct { double speed; double torque; double temp; double current; } motor; with suitable comments on the individual elements, as well as comment on the struct itself. The struct naturally shows the association. This was done for readability. -- Thad |
|
|
|
#15 |
|
Messages: n/a
Hébergeur: |
On Oct 18, 7:12 am, Eric Sosman <esos...@ieee-dot-org.invalid> wrote:
> ymunt...@gmail.com wrote: > > On Oct 17, 5:34 pm, Eric Sosman <esos...@ieee-dot-org.invalid> wrote: > >> Fred Phillips wrote: > >>> [...] > >>> 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? > > 6.7.2.3p4 says the types are distinct, but 6.2.7p1 > says they are compatible anyhow. Then 6.2.7p1 says they are the same anyhow, yet distinct according to 6.7.2.3p4. To me it looks like an error, possibility of two anonymous structure types in the same translation unit was missed (assuming this, 6.2.7p1 clearly as an explanation of why including a header with a structure declaration in two source files will work). Best regards, Yevgen |
|
|
|
#16 |
|
Messages: n/a
Hébergeur: |
Thad Smith <ThadSmith@acm.org> writes:
> 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?"] > >> 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. > > I wrote some simulation code recently that had a large module with > many file scope variables containing various states of the simulation. > I wanted to group several of them having to do with a particular > aspect, for example, motor. Rather than defining motorSpeed, > motorTorque, motorTemp, and motorCurrent, I choose to group them as > > static struct { > double speed; > double torque; > double temp; > double current; > } motor; > > with suitable comments on the individual elements, as well as comment > on the struct itself. The struct naturally shows the association. > This was done for readability. This is pretty much exactly what structs are for. I'm not sure I understand your point here unless it is to explain how to use structs? |
|
![]() |
| Outils de la discussion | |
|
|