Is this legal ?

M

main()

Hi all,

I wanted to have a union which has two structures in it.
So i did this,

union {
struct{
int a;
int b;
};
struct{
float c;
};
}un;

I did not want to name the two inner structures, because i wanted to
access the members like this,
<union name>.<member name>
instead of
<union name>.<structure name>.<member name>

My question is, Is this valid C?

I also observed that if i have a union like this ,

union {
struct{
int a;
int b;
};
struct{
float a;
float b;
};
}un;

Compiler doesn't give me any error.
If i say, 'un.a' how will it know whether i'm using float a or int
a ?


Thanks for your time,
Yugi.
 
S

Spiros Bousbouras

main() said:
Hi all,

I wanted to have a union which has two structures in it.
So i did this,

union {
struct{
int a;
int b;
};
struct{
float c;
};
}un;

I did not want to name the two inner structures, because i wanted to
access the members like this,
<union name>.<member name>
instead of
<union name>.<structure name>.<member name>

My question is, Is this valid C?

No. The compiler cannot read your mind and realize
that when you write <union name>.<member name>
you really mean said:
I also observed that if i have a union like this ,

union {
struct{
int a;
int b;
};
struct{
float a;
float b;
};
}un;

Compiler doesn't give me any error.
If i say, 'un.a' how will it know whether i'm using float a or int
a ?

It won't. Hence your definition is useless even if it
doesn't cause your compiler to complain. For the
programme

int main(void) {
union {
struct{
int a;
int b;
};
struct{
float a;
float b;
};
}un;
}

Sun compiler gives
"b.c", line 6: warning: unnamed union member
"b.c", line 10: warning: unnamed union member
"b.c", line 11: zero-sized struct/union
cc: acomp failed for b.c

gcc -Wall -ansi -pedantic b.c gives
b.c: In function `main':
b.c:6: warning: ANSI C forbids member declarations with no members
b.c:6: warning: unnamed struct/union that defines no instances
b.c:10: warning: ANSI C forbids member declarations with no members
b.c:10: warning: unnamed struct/union that defines no instances
b.c:11: warning: union has no members
b.c:11: warning: unused variable `un'
b.c:12: warning: control reaches end of non-void function

I would suggest that you turn your compiler's warning
levels up. If you still don't get a warning then it's time
to switch to a new compiler.
 
N

Naresh

Spiros said:
No. The compiler cannot read your mind and realize
that when you write <union name>.<member name>


It won't. Hence your definition is useless even if it
doesn't cause your compiler to complain. For the
programme

int main(void) {
union {
struct{
int a;
int b;
};
struct{
float a;
float b;
};
}un;
}

Sun compiler gives
"b.c", line 6: warning: unnamed union member
"b.c", line 10: warning: unnamed union member
"b.c", line 11: zero-sized struct/union
cc: acomp failed for b.c

gcc -Wall -ansi -pedantic b.c gives
b.c: In function `main':
b.c:6: warning: ANSI C forbids member declarations with no members
b.c:6: warning: unnamed struct/union that defines no instances
b.c:10: warning: ANSI C forbids member declarations with no members
b.c:10: warning: unnamed struct/union that defines no instances
b.c:11: warning: union has no members
b.c:11: warning: unused variable `un'
b.c:12: warning: control reaches end of non-void function

I would suggest that you turn your compiler's warning
levels up. If you still don't get a warning then it's time
to switch to a new compiler.


The main usage of Annonymous Union is to limit scope of some name
space. If you are defining Anno. union, you have to take care scope of
variable yourself.
 
J

jacob navia

This is not strictly valid C but it is a common extension.
lcc-win32 will accept this code, and it is practical since
it allows you to avoid specifying the whole "path" to
the member and giving you more flexibility in the
layout of the structire without changing the code.

jacob
 
S

Spiros Bousbouras

jacob said:
This is not strictly valid C but it is a common extension.
lcc-win32 will accept this code, and it is practical since
it allows you to avoid specifying the whole "path" to
the member and giving you more flexibility in the
layout of the structire without changing the code.

lcc will accept which code ? How are you going to
refer to members ? Examples please.
 
J

jacob navia

Spiros said:
jacob navia wrote:




lcc will accept which code ? How are you going to
refer to members ? Examples please.

union {
struct{
int a;
int b;
};
struct{
float c;
};
}un;

int main(void)
{

un.a = 1;
return 0;
}
 
R

Richard Heathfield

jacob navia said:
This is not strictly valid C but it is a common extension.
lcc-win32 will accept this code,

How can that possibly be relevant to the OP? Please keep your spam to
yourself.
 
J

jacob navia

Richard said:
jacob navia said:

MSVC will accept this code too.

gcc (in its normal, non pedantic configuration) will accept
this code too.

This means in almost ANY workstation today (windows, microsoft, or Mac)
this code is portable and will be accepted.

Only people that insist in restricting themselves to ISO C will
(after some effort) be unable to compile that code.
 
S

Spiros Bousbouras

jacob said:
union {
struct{
int a;
int b;
};
struct{
float c;
};
}un;

int main(void)
{

un.a = 1;
return 0;
}

Interesting. Can you say a bit about what
algorithm you use ? I mean does the compiler
search in arbitrary depth in structures/unions
which contain structures/unions as fields which
contain structures/unions as fields and so on, to
see if a field is uniquely named and if it is then it
will assume that this is the field which is being referred ?

How will the compiler react to the following ?

union {
struct{
int a;
int b;
};
struct{
float a;
float b;
};
}un;
 
R

Richard Bos

jacob navia said:
This is not strictly valid C but it is a common extension.
lcc-win32 will accept this code,

Yeah, but then, if you spit on a floppy disk your toy will probably
accept the result as perfectly good C with a boatload of happy happy
extensions, so that means nothing.

What the OP posted, and what you conveniently but luserishly snipped
completely, is not just "not strictly valid C"; it's strictly not valid
C. That's all he asked for. Advertisements were not requested.

Richard
 
J

jacob navia

Spiros said:
jacob navia wrote:




Interesting. Can you say a bit about what
algorithm you use ? I mean does the compiler
search in arbitrary depth in structures/unions
which contain structures/unions as fields which
contain structures/unions as fields and so on, to
see if a field is uniquely named and if it is then it
will assume that this is the field which is being referred ?

How will the compiler react to the following ?

union {
struct{
int a;
int b;
};
struct{
float a;
float b;
};
}un;

The algorithm is simple:

It requires that all field names (at any depths) are UNIQUE,
so your example will NOT work. The compiler will start a depth first
search in the whole nested structures with a field for that
name, and if it finds it it will automatically generate the
path to it.

I have the idea of this extensions from the Plan9 compiler
by Dennis Ritchie.

jacob
 
J

jacob navia

Richard said:
Yeah, but then, if you spit on a floppy disk your toy will probably
accept the result as perfectly good C with a boatload of happy happy
extensions, so that means nothing.

What the OP posted, and what you conveniently but luserishly snipped
completely, is not just "not strictly valid C"; it's strictly not valid
C. That's all he asked for. Advertisements were not requested.

Richard

Mr traffic cop:

The subject of the quetion was answered with my FIRST sentence:

"This is not strictly valid C".

Since most compilers in the workstation level (windows, unix, mac)
accept this code. MSVC AND GCC will accept it, maybe not in the
same level as lcc-win32 but this code below will be accepted by
all:


union {
struct{
int a;
int b;
};
struct{
float c;
};
}un;

int main(void)
{
un.a = 1;
return 0;
}

So, it is my right to express my opinion that this is a VERY common
extension.
 
S

Spiros Bousbouras

jacob said:
The algorithm is simple:

It requires that all field names (at any depths) are UNIQUE,
so your example will NOT work.

I meant will the compiler complain where the structure is declared
or will it complain at the point where the code tries to access some
member that the member name is not unique ?
 
J

jacob navia

Spiros said:
jacob navia wrote:




I meant will the compiler complain where the structure is declared
or will it complain at the point where the code tries to access some
member that the member name is not unique ?

It will complain at the point of the declaration.
Your example will produce with lcc-win32:
Error tanonymous.c: 10 ambiguous field 'a' of 'union defined at
d:\lcc\mc66\test\tanonymous.c 1' from 'struct defined at
d:\lcc\mc66\test\tanonymous.c 6'
Error tanonymous.c: 10 ambiguous field 'b' of 'union defined at
d:\lcc\mc66\test\tanonymous.c 1' from 'struct defined at
d:\lcc\mc66\test\tanonymous.c 6'
 
R

Richard Heathfield

jacob navia said:
Mr traffic cop:

The subject of the quetion was answered with my FIRST sentence:

"This is not strictly valid C".

And that should have been your *only* sentence. Everything after it was
utterly irrelevant to the OP's question.
So, it is my right to express my opinion that this is a VERY common
extension.

And it's my right to express my opinion that you seem to be desperate for
any chance to promote your squalid little implementation, because if you
don't, people might forget it's there.

So let's keep our opinions to ourselves and just talk about C, shall we?

And for the record, lcc-win32 is *not* C, and C is *not* lcc-win32. C, the
subject of this newsgroup, is a *language*, not an implementation.
 
K

Keith Thompson

jacob navia said:
union {
struct{
int a;
int b;
};
struct{
float c;
};
}un;

int main(void)
{

un.a = 1;
return 0;
}

lcc-win32 doesn't accept this code in conforming mode, does it?
 
J

jacob navia

Richard said:
And it's my right to express my opinion that you seem to be desperate for
any chance to promote your squalid little implementation, because if you
don't, people might forget it's there.


We hit for the first time a download rate of
more than 1 000 downloads per day yesterday.

This rate was measured in one of our download sites
(www-q-software-solutions.de) without adding the
downloads from
http://www.cs.virginia.edu/~lcc-win32.

Thanks to all for this support.

:)


jacob
 
J

jacob navia

Keith said:
lcc-win32 doesn't accept this code in conforming mode, does it?

The problem is that all windows header files use this. I DID emit an
error for this, but people complained that then you could NOT
compile a windows program in conforming mode, what made the
conforming mode useless for all practical purposes.

Then, I took out the error and made this exception. I do not
see any way out of this dilemma.

jacob
 
R

Richard Heathfield

jacob navia said:
We hit for the first time a download rate of
more than 1 000 downloads per day yesterday.

Gosh. Your mouse-finger must be all worn out.
 

Ask a Question

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.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top