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 17/10/2007, 22h12   #1
Fred Phillips
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut What's the use of anonymous structs?

Title says it all...

  Réponse avec citation
Vieux 17/10/2007, 22h45   #2
Richard Heathfield
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: What's the use of anonymous structs?

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
  Réponse avec citation
Vieux 17/10/2007, 22h51   #3
robertwessel2@yahoo.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: What's the use of anonymous structs?

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.

  Réponse avec citation
Vieux 17/10/2007, 22h56   #4
robertwessel2@yahoo.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: What's the use of anonymous structs?

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...

  Réponse avec citation
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
Vieux 17/10/2007, 23h15   #6
Keith Thompson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: What's the use of anonymous structs?

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"
  Réponse avec citation
Vieux 17/10/2007, 23h34   #7
Eric Sosman
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?"]

>> 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
  Réponse avec citation
Vieux 18/10/2007, 05h36   #8
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 ...


Doesn't this contradict 6.7.2.3p4?

Best regards,
Yevgen

  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 06h15.


Édité par : vBulletin® version 3.7.2
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
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,13687 seconds with 16 queries