Struct declaration with no tag and no init-declarator-list

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

    Struct declaration with no tag and no init-declarator-list

    The following declaration is valid:

    struct {int x;};

    There's no tag and no variable.

    Does it has any use?

    Thanks.

    Joey.
  • viza

    #2
    Re: Struct declaration with no tag and no init-declarator-list

    Hi

    On Thu, 12 Jun 2008 08:19:58 -0700, JoseMariaSola wrote:
    struct {int x;};
    >
    There's no tag and no variable.
    >
    Does it has any use?
    On it's own, it does nothing. Inside another struct or union, its
    member(s) can be accessed as if they were members of the parent. eg:

    struct {
    struct {int x;};
    }
    foo;

    foo. x= 7;

    which is easier than:

    struct {
    struct {int x;} bar;
    }
    foo;

    foo. bar. x= 7;

    HTH

    viza

    Comment

    • viza

      #3
      Re: Struct declaration with no tag and no init-declarator-list

      On Thu, 12 Jun 2008 15:25:36 +0000, viza wrote:
      >struct {int x;};
      >
      On it's own, it does nothing. Inside another struct or union, its
      member(s) can be accessed as if they were members of the parent. eg:
      PS: I didn't realise that that's non-standard. Flames may follow...

      Comment

      • Harald van =?UTF-8?b?RMSzaw==?=

        #4
        Re: Struct declaration with no tag and no init-declarator-list

        On Thu, 12 Jun 2008 08:19:58 -0700, JoseMariaSola wrote:
        The following declaration is valid:
        >
        struct {int x;};
        No, it isn't. It's not a syntax error, but C requires a declaration to
        actually declare something.

        Comment

        • Keith Thompson

          #5
          Re: Struct declaration with no tag and no init-declarator-list

          viza <tom.viza@gmil. comwrites:
          On Thu, 12 Jun 2008 08:19:58 -0700, JoseMariaSola wrote:
          >
          >struct {int x;};
          >>
          >There's no tag and no variable.
          >>
          >Does it has any use?
          >
          On it's own, it does nothing. Inside another struct or union, its
          member(s) can be accessed as if they were members of the parent. eg:
          >
          struct {
          struct {int x;};
          }
          foo;
          >
          foo. x= 7;
          >
          which is easier than:
          >
          struct {
          struct {int x;} bar;
          }
          foo;
          >
          foo. bar. x= 7;
          No, you can't, at least not in standard C. Some compilers may offer
          it as an extension. But if you want your code to be portable, you'll
          just have to write "foo.bar.x" (or do some ugly macro stuff).

          --
          Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
          Nokia
          "We must do something. This is something. Therefore, we must do this."
          -- Antony Jay and Jonathan Lynn, "Yes Minister"

          Comment

          • JoseMariaSola

            #6
            Re: Struct declaration with no tag and no init-declarator-list

            The following declaration is valid:
            >
            struct {int x;};
            >
            No, it isn't. It's not a syntax error, but C requires a declaration to
            actually declare something.
            Is it a static semantic error? My compiler doesn't detect it. Is it
            broken?
            In the Standard I didn't find any restriction.

            Comment

            • Harald van =?UTF-8?b?RMSzaw==?=

              #7
              Re: Struct declaration with no tag and no init-declarator-list

              On Thu, 12 Jun 2008 09:13:27 -0700, JoseMariaSola wrote:
              The following declaration is valid:
              >>
              struct {int x;};
              >>
              >No, it isn't. It's not a syntax error, but C requires a declaration to
              >actually declare something.
              >
              Is it a static semantic error?
              It's a constraint violation, which requires a diagnostic from any
              conforming implementation.
              My compiler doesn't detect it. Is it
              broken?
              If you asked it to try to conform to the standard, then yes.
              In the Standard I didn't find any restriction.
              It's in 6.7p2: "A declaration shall declare at least a declarator (other
              than the parameters of a function or the members of a structure or union),
              a tag, or the members of an enumeration." Your declaration does not
              declare any of those.

              Comment

              • JoseMariaSola

                #8
                Re: Struct declaration with no tag and no init-declarator-list

                On Jun 12, 1:16 pm, Harald van D©¦k <true...@gmail. comwrote:
                On Thu, 12 Jun 2008 09:13:27 -0700, JoseMariaSola wrote:
                The following declaration is valid:
                >
                struct {int x;};
                >
                No, it isn't. It's not a syntax error, but C requires a declaration to
                actually declare something.
                >
                Is it a static semantic error?
                >
                It's a constraint violation, which requires a diagnostic from any
                conforming implementation.
                >
                My compiler doesn't detect it. Is it
                broken?
                >
                If you asked it to try to conform to the standard, then yes.
                >
                In the Standard I didn't find any restriction.
                >
                It's in 6.7p2: "A declaration shall declare at least a declarator (other
                than the parameters of a function or the members of a structure or union),
                a tag, or the members of an enumeration." Your declaration does not
                declare any of those.
                Thanks!

                Comment

                Working...