Is this a valid struct declaration

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Raman

    Is this a valid struct declaration

    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
  • rahul

    #2
    Re: Is this a valid struct declaration

    On Jun 6, 2:32 pm, Raman <ramanchalo...@ gmail.comwrote:
    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.

    Comment

    • Ben Bacarisse

      #3
      Re: Is this a valid struct declaration

      rahul <rahulsinner@gm ail.comwrites:
      On Jun 6, 2:32 pm, Raman <ramanchalo...@ gmail.comwrote:
      <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.

      Comment

      • Flash Gordon

        #4
        Re: Is this a valid struct declaration

        rahul wrote:
        On Jun 6, 2:32 pm, Raman <ramanchalo...@ gmail.comwrote:
        >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

        Comment

        • muks

          #5
          Re: Is this a valid struct declaration

          On Jun 6, 2:51 pm, rahul <rahulsin...@gm ail.comwrote:
          On Jun 6, 2:32 pm, Raman <ramanchalo...@ gmail.comwrote:
          >
          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

          Comment

          • Jack Klein

            #6
            Re: Is this a valid struct declaration

            On Fri, 6 Jun 2008 06:45:27 -0700 (PDT), muks <mukul2cool@gma il.com>
            wrote in comp.lang.c:
            On Jun 6, 2:51 pm, rahul <rahulsin...@gm ail.comwrote:
            On Jun 6, 2:32 pm, Raman <ramanchalo...@ gmail.comwrote:
            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
            hey ur a fkng mron.

            --
            Jack Klein
            Home: http://JK-Technology.Com
            FAQs for
            comp.lang.c http://c-faq.com/
            comp.lang.c++ http://www.parashift.com/c++-faq-lite/
            alt.comp.lang.l earn.c-c++

            Comment

            • Chris Thomasson

              #7
              Re: Is this a valid struct declaration


              "muks" <mukul2cool@gma il.comwrote in message
              news:d913f2f4-92f1-4ec8-82fb-ff2f557c00c9@a3 2g2000prf.googl egroups.com...
              [...]
              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
              :^/

              Comment

              • Dan

                #8
                Re: Is this a valid struct declaration

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


                Comment

                • Ian Collins

                  #9
                  Re: Is this a valid struct declaration

                  rahul wrote:
                  On Jun 6, 2:32 pm, Raman <ramanchalo...@ gmail.comwrote:
                  >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.

                  Comment

                  • Raman

                    #10
                    Re: Is this a valid struct declaration

                    On Jun 6, 4:56 pm, Flash Gordon <s...@flash-gordon.me.ukwro te:
                    rahul wrote:
                    On Jun 6, 2:32 pm, Raman <ramanchalo...@ gmail.comwrote:
                    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(incomple te), every element
                    having same mem address....whic h is probably violation of array
                    concept in itself.

                    Thanks,
                    Raman Chalotra

                    Comment

                    • Flash Gordon

                      #11
                      Re: Is this a valid struct declaration

                      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(incomple te), every element
                      having same mem address....whic h 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

                      Comment

                      Working...