different behavior of empty struct & enum

T

Tagore

Hi,
when I try to declare an empty structure, there is no error as in
below
struct a{

};

however, when I try to declare an empty enum it shows error as in
enum a{

};

Why are different behaviors in case of struct & enum? Also I would
like to know what could be use of such an empty structure?

Thanks,
 
U

Uno

Richard said:
The above is a syntax error.



None, in C, since C doesn't support empty structures.

If your implementation doesn't diagnose an empty structure, you are not
invoking it in its conforming mode.

OT

In America, we use empty structures like Sarah Palin and George Bush to
make certain our standards are maximally low.

Is Great Britain in anarchy since recent common-sense financial regulation?

~OT
 
I

Igmar Palsenberg

Hi,
when I try to declare an empty structure, there is no error as in
below
struct a{

};

Empty structs are a gcc extension. The language specs forbid them. See
the warning when compiling with -pedantic
however, when I try to declare an empty enum it shows error as in
enum a{

};

The language also forbids the above. It also makes no sense, hence the
syntax error.
Why are different behaviors in case of struct& enum? Also I would
like to know what could be use of such an empty structure?

Saving a byte that is unneeded is one answer. I never had to use them.



Igmar
 
P

Phil Carmody

Igmar Palsenberg said:
Empty structs are a gcc extension. The language specs forbid them. See
the warning when compiling with -pedantic


The language also forbids the above. It also makes no sense, hence the
syntax error.


Saving a byte that is unneeded is one answer. I never had to use them.

I think it's more likely to overcome the idiocy of the sizeof(void)=1
choice. Finally you've got something that has sizeof 0:

#include <stdio.h>
struct foo
{
} a;
int main()
{
printf("empty struct has size %i\n", (int)sizeof(a));
printf("void has size %i\n", (int)sizeof(void));
return 0;
}

$ gcc -Wall -o crap crap.c
$ ./crap
empty struct has size 0
void has size 1

Phil
 
U

Uno

Phil said:
I think it's more likely to overcome the idiocy of the sizeof(void)=1
choice. Finally you've got something that has sizeof 0:

#include <stdio.h>
struct foo
{
} a;
int main()
{
printf("empty struct has size %i\n", (int)sizeof(a));
printf("void has size %i\n", (int)sizeof(void));
return 0;
}

$ gcc -Wall -o crap crap.c
$ ./crap
empty struct has size 0
void has size 1

$ gcc -Wall -Wextra p1.c -o nichtfinnisch
p1.c:12: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before
‘gcc’
$ cat p1.c
#include <stdio.h>
struct foo
{
} a;
int main()
{
printf("empty struct has size %i\n", (int)sizeof(a));
printf("void has size %i\n", (int)sizeof(void));
return 0;
}

$ gcc -Wall -Wextra p1.c -o nichtfinnisch
$

I seem to have done enough fortran recently to be rusty on C.

Tja.
 
K

Keith Thompson

Kenneth Brody said:
On 4/27/2010 5:19 AM, Phil Carmody wrote:
[...]
#include<stdio.h>
struct foo
{
} a;
int main()
{
printf("empty struct has size %i\n", (int)sizeof(a));
printf("void has size %i\n", (int)sizeof(void));
return 0;
}

$ gcc -Wall -o crap crap.c
$ ./crap
empty struct has size 0
void has size 1

FYI -

My compiler fails with the error:

C requires that a struct or union has at least one member

and the warning:

sizeof returns 0

Removing the empty struct and corresponding printf gives the output:

void has size 0

Do you mean a compiler you wrote, or the compiler that you use? Which
compiler is it?

Why does sizeof(void) merely produce a warning rather than a fatal
error? It's perfectly legal either way, of course; as long as the
compiler produces a diagnostic, it's conforming. But in this case,
IMHO, it should be rejected.
 
K

Keith Thompson

Kenneth Brody said:
Kenneth Brody said:
On 4/27/2010 5:19 AM, Phil Carmody wrote: [... empty structs, and sizeof(void) ...]
My compiler fails with the error:

C requires that a struct or union has at least one member

and the warning:

sizeof returns 0

Removing the empty struct and corresponding printf gives the output:

void has size 0

Do you mean a compiler you wrote, or the compiler that you use? Which
compiler is it?

I was referring to the one I used on the computer I was sitting in front of.
In the case at hand, it's "Microsoft (R) 32-bit C/C++ Optimizing Compiler
Version 16.00.30319.01 for 80x86".
Why does sizeof(void) merely produce a warning rather than a fatal
error? It's perfectly legal either way, of course; as long as the
compiler produces a diagnostic, it's conforming. But in this case,
IMHO, it should be rejected.

Umm... Don't ask me why it's only a warning, I didn't write the compiler.
Perhaps that's for people porting from gcc? (Though, if that were the case,
I would think they would treat it as 1 and not zero, to prevent bug reports
claiming the MS compiler was "broken".) Does gcc issue a diagnostic in
conforming mode?

By definition, yes. :cool:}

With "-pedantic" gcc says "warning: invalid application of 'sizeof' to a
void type". (If gcc didn't issue *some* diagnostic, it would be
non-conforming.)
On the other hand, I think it would be good if "sizeof char" generated a
diagnostic as well. (Is this allowed in a conforming compiler?)

``sizeof char'' does generate a diagnostic, since it's a syntax error.
``sizeof (char)'', on the other hand ...

Yes, a conforming compiler can issue diagnostics (warnings) whenever it
likes. I don't think I'd want a warning for sizeof(char) by default,
since there are reasonable uses for it (consider a macro that takes a
type name as an argument). But an option to enable style warnings could
reasonably warn about it.
 

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

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,050
Latest member
AngelS122

Latest Threads

Top