different behavior of empty struct & enum

Discussion in 'C Programming' started by Tagore, Apr 27, 2010.

  1. Tagore

    Tagore Guest

    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,
     
    Tagore, Apr 27, 2010
    #1
    1. Advertisements

  2. Tagore

    Uno Guest

    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
     
    Uno, Apr 27, 2010
    #2
    1. Advertisements

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



    Igmar
     
    Igmar Palsenberg, Apr 27, 2010
    #3
  4. Tagore

    Phil Carmody Guest

    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
     
    Phil Carmody, Apr 27, 2010
    #4
  5. Tagore

    Uno Guest

    $ 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.
     
    Uno, Apr 27, 2010
    #5
  6. 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.
     
    Keith Thompson, Apr 28, 2010
    #6
  7. 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.)
    ``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.
     
    Keith Thompson, Apr 28, 2010
    #7
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.