Is this a valid struct declaration

Discussion in 'C Programming' started by Raman, Jun 6, 2008.

  1. Raman

    Raman Guest

    Hi All,


    Is it valid:

    struct test{

    };

    I mean, Can we have a struct containing no members? Is this a an
    incomplete-type?

    Please provide your expert comments.

    Thanks,
    Raman Chalotra
     
    Raman, Jun 6, 2008
    #1
    1. Advertising

  2. Raman

    rahul Guest

    On Jun 6, 2:32 pm, Raman <> wrote:
    > Hi All,
    >
    > Is it valid:
    >
    > struct test{
    >
    > };
    >
    > I mean, Can we have a struct containing no members? Is this a an
    > incomplete-type?
    >
    > Please provide your expert comments.
    >
    > Thanks,
    > Raman Chalotra


    I don't see anything wrong with the declaration. Though this stuff
    does not look like it can have any practical applications.
    The standard does not mandate declaring a member in a structure. This
    code compiles with '-ansi' flag on Linux/gcc.
    I don't see any reason why it should not be valid.
     
    rahul, Jun 6, 2008
    #2
    1. Advertising

  3. rahul <> writes:

    > On Jun 6, 2:32 pm, Raman <> wrote:

    <snip>
    >> struct test{
    >>
    >> };
    >>
    >> I mean, Can we have a struct containing no members? Is this a an
    >> incomplete-type?
    >>
    >> Please provide your expert comments.
    >>
    >> Thanks,
    >> Raman Chalotra

    >
    > I don't see anything wrong with the declaration. Though this stuff
    > does not look like it can have any practical applications.
    > The standard does not mandate declaring a member in a structure.


    My copy does. The syntax specifies that there must be at least one
    member. Where are you getting your information from?

    > This
    > code compiles with '-ansi' flag on Linux/gcc.


    Add -pedantic.

    > I don't see any reason why it should not be valid.


    --
    Ben.
     
    Ben Bacarisse, Jun 6, 2008
    #3
  4. Raman

    Flash Gordon Guest

    rahul wrote:
    > On Jun 6, 2:32 pm, Raman <> wrote:
    >> Hi All,
    >>
    >> Is it valid:
    >>
    >> struct test{
    >>
    >> };
    >>
    >> I mean, Can we have a struct containing no members?


    No.

    >> Is this a an
    >> incomplete-type?


    No.

    >> Please provide your expert comments.
    >>
    >> Thanks,
    >> Raman Chalotra

    >
    > I don't see anything wrong with the declaration.


    Then you need to look at the standard more carefully:
    6.7.2.1 Structure and union specifiers
    Syntax
    1 struct-or-union-specifier:
    struct-or-union identifieropt { struct-declaration-list }
    struct-or-union identifier

    struct-or-union:
    struct
    union

    struct-declaration-list:
    struct-declaration
    struct-declaration-list struct-declaration

    Note that a struct-declaration-list ALWAYS has a struct-declaration!

    > Though this stuff
    > does not look like it can have any practical applications.
    > The standard does not mandate declaring a member in a structure.


    Wrong. From N1256:


    > This
    > code compiles with '-ansi' flag on Linux/gcc.


    That does not make it procude all required diagnostics. You need '-ansi
    -pedantic' at which point it gives a warning. Note that a warning is
    sufficient to meet the standards requirement for "invalid" code (a puch
    in the face could also qualify, but I don't think an implementation that
    did this would be very popular)

    > I don't see any reason why it should not be valid.


    See above.

    An imcomplete type would be
    struct test;
    --
    Flash Gordon
     
    Flash Gordon, Jun 6, 2008
    #4
  5. Raman

    muks Guest

    On Jun 6, 2:51 pm, rahul <> wrote:
    > On Jun 6, 2:32 pm, Raman <> wrote:
    >
    > > Hi All,

    >
    > > Is it valid:

    >
    > > struct test{

    >
    > > };

    >
    > > I mean, Can we have a struct containing no members? Is this a an
    > > incomplete-type?

    >
    > > Please provide your expert comments.

    >
    > > Thanks,
    > > Raman Chalotra

    >
    > I don't see anything wrong with the declaration. Though this stuff
    > does not look like it can have any practical applications.
    > The standard does not mandate declaring a member in a structure. This
    > code compiles with '-ansi' flag on Linux/gcc.
    > I don't see any reason why it should not be valid.


    hey i dont find ne thng wrong in ur problem..........bt surely this
    thng cannot b realised in practical situation......rest its ok.tk cr
    bye
     
    muks, Jun 6, 2008
    #5
  6. Raman

    Dan Guest


    >hey i dont find ne thng wrong in ur problem..........bt surely this
    >thng cannot b realised in practical situation......rest its ok.tk cr
    >bye


    I have seen #defines that turn on and off structure members to save memory,
    and one even turned all the members off.
     
    Dan, Jun 8, 2008
    #6
  7. Raman

    Ian Collins Guest

    rahul wrote:
    > On Jun 6, 2:32 pm, Raman <> wrote:
    >> Hi All,
    >>
    >> Is it valid:
    >>
    >> struct test{
    >>
    >> };
    >>
    >> I mean, Can we have a struct containing no members? Is this a an
    >> incomplete-type?
    >>
    >> Please provide your expert comments.
    >>
    >> Thanks,
    >> Raman Chalotra

    >
    > I don't see anything wrong with the declaration. Though this stuff
    > does not look like it can have any practical applications.
    > The standard does not mandate declaring a member in a structure. This
    > code compiles with '-ansi' flag on Linux/gcc.
    > I don't see any reason why it should not be valid.


    Are you sure you weren't compiling as C++?

    --
    Ian Collins.
     
    Ian Collins, Jun 8, 2008
    #7
  8. Raman

    Raman Guest

    On Jun 6, 4:56 pm, Flash Gordon <> wrote:
    > rahul wrote:
    > > On Jun 6, 2:32 pm, Raman <> wrote:
    > >> Hi All,

    >
    > >> Is it valid:

    >
    > >> struct test{

    >
    > >> };

    >
    > >> I mean, Can we have a struct containing no members?

    >
    > No.
    >
    > >> Is this a an
    > >> incomplete-type?

    >
    > No.
    >
    > >> Please provide your expert comments.

    >
    > >> Thanks,
    > >> Raman Chalotra

    >
    > > I don't see anything wrong with the declaration.

    >
    > Then you need to look at the standard more carefully:
    > 6.7.2.1 Structure and union specifiers
    > Syntax
    > 1 struct-or-union-specifier:
    > struct-or-union identifieropt { struct-declaration-list }
    > struct-or-union identifier
    >
    > struct-or-union:
    > struct
    > union
    >
    > struct-declaration-list:
    > struct-declaration
    > struct-declaration-list struct-declaration
    >
    > Note that a struct-declaration-list ALWAYS has a struct-declaration!
    >
    > > Though this stuff
    > > does not look like it can have any practical applications.
    > > The standard does not mandate declaring a member in a structure.

    >
    > Wrong. From N1256:
    >
    > > This
    > > code compiles with '-ansi' flag on Linux/gcc.

    >
    > That does not make it procude all required diagnostics. You need '-ansi
    > -pedantic' at which point it gives a warning. Note that a warning is
    > sufficient to meet the standards requirement for "invalid" code (a puch
    > in the face could also qualify, but I don't think an implementation that
    > did this would be very popular)
    >
    > > I don't see any reason why it should not be valid.

    >
    > See above.
    >
    > An imcomplete type would be
    > struct test;
    > --
    > Flash Gordon


    HI,

    Yes, and
    1. we cant apply sizeof to incomplete types.
    2. what if we create array of this struct(incomplete), every element
    having same mem address....which is probably violation of array
    concept in itself.

    Thanks,
    Raman Chalotra
     
    Raman, Jun 9, 2008
    #8
  9. Raman

    Flash Gordon Guest

    Raman wrote, On 09/06/08 10:41:

    <snip>

    > Yes, and
    > 1. we cant apply sizeof to incomplete types.


    True.

    > 2. what if we create array of this struct(incomplete), every element
    > having same mem address....which is probably violation of array
    > concept in itself.


    It is an error that the compiler is required to diagnose (produce a
    warning, error or tattoo the appropriate clause of the standard on some
    part of your body for).
    --
    Flash Gordon
    Inventor of strange and unusual diagnostics
     
    Flash Gordon, Jun 9, 2008
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Carlos
    Replies:
    1
    Views:
    372
    Wilco Bauwer
    Feb 17, 2005
  2. Ray Tayek
    Replies:
    13
    Views:
    2,970
    Ray Tayek
    Dec 2, 2003
  3. Chris Fogelklou
    Replies:
    36
    Views:
    1,441
    Chris Fogelklou
    Apr 20, 2004
  4. Ehud Shapira
    Replies:
    20
    Views:
    1,184
    Ehud Shapira
    Jun 30, 2007
  5. slocum
    Replies:
    3
    Views:
    519
    slocum
    Apr 11, 2008
Loading...

Share This Page