F
Fred Phillips
Title says it all...
Title says ["What's the use of anonymous structs?"]
Title says it all...
What's the use of anonymous structs?
As far as I can discern, C doesn't provide such a feature.
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 said: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.
Fred said:Fred Phillips said:
As far as I can discern, C doesn't provide such a feature. So theTitle says ["What's the use of anonymous structs?"]
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.
[...]
Fred said: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 ...
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;
So what's the point of allowing anonymous structs if they can't be
passed around in functions or assigned between each other?
Fred said: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")
Title says it all...
It's compatible with an identically-declared struct inFred said:[...]
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.
[...]
a different module ...
Doesn't this contradict 6.7.2.3p4?
In the future, please include your question in the body of the post.
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 */
}
struct bar {int a; union {int aa; double bb; long cc;}; int c;};
Which then allows:
struct bar sb;
...
sb.bb = 1.0;
Fred said: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.
[email protected] said: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.
Thad Smith said:Fred said: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.
[email protected] said: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).
Martien Verbruggen said: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?
It is a Microsoft extension to C. GCC also supports it, with
-fms-extensions (see last paragraph below):
5.50 Unnamed struct/union fields within structs/unions
======================================================
For compatibility with other compilers, GCC allows you to define a
structure or union that contains, as fields, structures and unions
without names. For example:
struct {
int a;
union {
int b;
float c;
};
int d;
} foo;
In this example, the user would be able to access members of the
unnamed union with code like `foo.b'. Note that only unnamed structs
and unions are allowed, you may not have, for example, an unnamed `int'.
You must never create such structures that cause ambiguous field
definitions. For example, this structure:
struct {
int a;
struct {
int a;
};
} foo;
It is ambiguous which `a' is being referred to with `foo.a'. Such
constructs are not supported and must be avoided. In the future, such
constructs may be detected and treated as compilation errors.
Unless `-fms-extensions' is used, the unnamed field must be a
structure or union definition without a tag (for example, `struct { int
a; };'). If `-fms-extensions' is used, the field may also be a
definition with a tag such as `struct foo { int a; };', a reference to
a previously defined structure or union such as `struct foo;', or a
reference to a `typedef' name for a previously defined structure or
union type.
(e-mail address removed) wrote On 10/18/07 09:37,:
(e-mail address removed) 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.
No, 6.2.7p1 says they are *compatible* types, not that
they are the *same* type.
I doubt the possibility was "missed." It seems more
likely that making the two tagless types distinct was
purposeful. If the programmer wrote two declarations
rather than just one, he gets two types and not just one.
In particular, the second declaration is not an (illegal)
attempt to re-declare the type of the first one.
Cross-module compatibility addresses a nasty issue
that confronts many separately-compilable languages. If
the compiler processes module A on Tuesday and processes
module B on Wednesday, we can't really talk about a type
compiled in A being "the same" as a type declared in B.
And yet, we want A to create objects of type T and pass
them to B to be processed. Since A and B had separate
declarations for T they can't really have "the same" T,
so the Standard resorts to this notion of "compatibility"
to allow separately-compiled modules to agree on what a
T looks like.
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.