PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.c > What's the use of anonymous structs?
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
What's the use of anonymous structs?

Réponse
 
LinkBack Outils de la discussion
Vieux 18/10/2007, 06h15   #9
Richard Heathfield
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: What's the use of anonymous structs?

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
  Réponse avec citation
Vieux 18/10/2007, 09h29   #10
ymuntyan@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: What's the use of anonymous structs?

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

  Réponse avec citation
Vieux 18/10/2007, 11h22   #11
sureshkumar.manimuthu@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: What's the use of anonymous structs?

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;
};

  Réponse avec citation
Vieux 18/10/2007, 13h12   #12
Eric Sosman
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: What's the use of anonymous structs?

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
  Réponse avec citation
Vieux 18/10/2007, 14h13   #13
Martien Verbruggen
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: What's the use of anonymous structs?

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.
|
  Réponse avec citation
Vieux 18/10/2007, 14h26   #14
Thad Smith
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: What's the use of anonymous structs?

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
  Réponse avec citation
Vieux 18/10/2007, 14h37   #15
ymuntyan@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: What's the use of anonymous structs?

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

  Réponse avec citation
Vieux 18/10/2007, 14h39   #16
Richard
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: What's the use of anonymous structs?

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?
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 12h45.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,14731 seconds with 16 queries