Unions in structures

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

    Unions in structures

    I recently noticed (I'm new to the language) that when using a union
    type as a component of a structure, you don't need a union-tag.

    struct foo
    {
    char *ptr;
    union
    {
    int i;
    float f;
    char c;
    };
    };

    I have never read that a union type reference is optional in any
    case.
    Is this specific to the compiler, or part of the standard?
  • Eric Sosman

    #2
    Re: Unions in structures

    blockstack wrote:
    I recently noticed (I'm new to the language) that when using a union
    type as a component of a structure, you don't need a union-tag.
    >
    struct foo
    {
    char *ptr;
    union
    {
    int i;
    float f;
    char c;
    };
    };
    >
    I have never read that a union type reference is optional in any
    case.
    Is this specific to the compiler, or part of the standard?
    A union does not need to have a union tag, and a struct
    does not need to have a struct tag. But I think you may be
    mixing up the terminology!

    - A "struct tag" is an identifier that can be combined
    with the keyword `struct' to form a name for a specific
    struct type. In your example, `foo' is a struct tag
    and `struct foo' is the name of the type. If the `foo'
    were absent, the struct would have no tag and there
    would be no way to write a name for its type elsewhere
    in the program.

    - Similarly, a "union tag" is an identifier that can be
    combined with the keyword `union' to form a name for a
    specific union type. In your example, the union has no
    tag and there is no way to write the name of the union's
    type. If the identifier `bar' appeared right after the
    `union' keyword, `bar' would be the union tag and
    `union bar' would be the name of the type.

    - Almost every element contained in a struct or union (the
    lone exception is not important here) must have an element
    name. In your example, `i', `f', and `c' are the names
    of the elements of the union. `ptr' is the name of one
    element of the struct, but the other element (the union)
    has no name. This is a compile-time error for which the
    compiler is required to produce a diagnostic message.

    So, why do you get no error message? Probably because you
    are not using a C compiler, but a C-with-extras compiler that
    accepts a C-like language that isn't really C. The gcc compiler
    is like that in its default mode of operation; command-line flags
    like `-ansi -pedantic' can make it conform more strictly to the
    actual definition of C.

    --
    Eric Sosman
    esosman@ieee-dot-org.invalid

    Comment

    • Morris Dovey

      #3
      Re: Unions in structures

      blockstack wrote:
      >
      I recently noticed (I'm new to the language) that when using a union
      type as a component of a structure, you don't need a union-tag.
      >
      struct foo
      {
      char *ptr;
      union
      {
      int i;
      float f;
      char c;
      };
      };
      >
      I have never read that a union type reference is optional in any
      case.
      Is this specific to the compiler, or part of the standard?
      A footnote to Eric's response:

      This convention produces headaches when/if struct foo is expanded
      to:

      struct foo
      { char *ptr;
      unsigned i;
      union
      { int i;
      float f;
      char c;
      };
      };

      struct foo x;
      x.i = 7; /* Which i in x? */

      --
      Morris Dovey
      DeSoto Solar
      DeSoto, Iowa USA

      Comment

      • blockstack

        #4
        Re: Unions in structures

        On Mar 16, 12:58 pm, Eric Sosman <esos...@ieee-dot-org.invalidwrot e:
        blockstack wrote:
        I recently noticed (I'm new to the language) that when using a union
        type as a component of a structure, you don't need a union-tag.
        >
        struct foo
        {
        char *ptr;
        union
        {
        int i;
        float f;
        char c;
        };
        };
        >
        I have never read that a union type reference is optional in any
        case.
        Is this specific to the compiler, or part of the standard?
        >
        A union does not need to have a union tag, and a struct
        does not need to have a struct tag. But I think you may be
        mixing up the terminology!
        >
        - A "struct tag" is an identifier that can be combined
        with the keyword `struct' to form a name for a specific
        struct type. In your example, `foo' is a struct tag
        and `struct foo' is the name of the type. If the `foo'
        were absent, the struct would have no tag and there
        would be no way to write a name for its type elsewhere
        in the program.
        >
        - Similarly, a "union tag" is an identifier that can be
        combined with the keyword `union' to form a name for a
        specific union type. In your example, the union has no
        tag and there is no way to write the name of the union's
        type. If the identifier `bar' appeared right after the
        `union' keyword, `bar' would be the union tag and
        `union bar' would be the name of the type.
        >
        - Almost every element contained in a struct or union (the
        lone exception is not important here) must have an element
        name. In your example, `i', `f', and `c' are the names
        of the elements of the union. `ptr' is the name of one
        element of the struct, but the other element (the union)
        has no name. This is a compile-time error for which the
        compiler is required to produce a diagnostic message.
        >
        So, why do you get no error message? Probably because you
        are not using a C compiler, but a C-with-extras compiler that
        accepts a C-like language that isn't really C. The gcc compiler
        is like that in its default mode of operation; command-line flags
        like `-ansi -pedantic' can make it conform more strictly to the
        actual definition of C.
        >
        --
        Eric Sosman
        esos...@ieee-dot-org.invalid
        Thanks,
        Using the option --ansi=pedantic did cause an error. I used union-tag
        and union-type-reference synonymously as I thought both were
        identifiers, but I see how I needed to make the distinction.
        The reason I was asking was because I was reading an old K&R C book
        from the 80s in which they were describing the previous code as a
        union variation rather than a structure. And then I received no error,
        so I wondered if this was part of the standard.
        But it is not standard C. It does seem to make things easier though.

        Comment

        • Keith Thompson

          #5
          Re: Unions in structures

          blockstack <blockstack@gma il.comwrites:
          I recently noticed (I'm new to the language) that when using a union
          type as a component of a structure, you don't need a union-tag.
          >
          struct foo
          {
          char *ptr;
          union
          {
          int i;
          float f;
          char c;
          };
          };
          >
          I have never read that a union type reference is optional in any
          case.
          Is this specific to the compiler, or part of the standard?
          It's specific to the compiler. As far as the standard is concerned,
          the above is an error requiring a diagnostic. Some compilers allow
          anonymous unions as an extension.

          In particular, gcc supports this. gcc's "-pedantic" option will cause
          it to issue a warning.

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

          Comment

          • Ben Pfaff

            #6
            Re: Unions in structures

            Eric Sosman <esosman@ieee-dot-org.invalidwrit es:
            The uses I've seen, though, have
            all been of a somewhat dubious nature, along the lines of
            >
            struct thing {
            struct {
            unsigned int readyFlag : 1;
            unsigned int willingFlag : 1;
            unsigned int ableFlag : 1;
            }; /* no name */
            unsigned int flags;
            #define READY_FLAG 0x1
            #define WILLING_FLAG 0x2
            #define ABLE_FLAG 0x4
            };
            I think that "struct thing" should be "union thing".
            --
            "When I have to rely on inadequacy, I prefer it to be my own."
            --Richard Heathfield

            Comment

            • Eric Sosman

              #7
              Re: Unions in structures

              Ben Pfaff wrote:
              Eric Sosman <esosman@ieee-dot-org.invalidwrit es:
              >
              >The uses I've seen, though, have
              >all been of a somewhat dubious nature, along the lines of
              >>
              > struct thing {
              > struct {
              > unsigned int readyFlag : 1;
              > unsigned int willingFlag : 1;
              > unsigned int ableFlag : 1;
              > }; /* no name */
              > unsigned int flags;
              > #define READY_FLAG 0x1
              > #define WILLING_FLAG 0x2
              > #define ABLE_FLAG 0x4
              > };
              >
              I think that "struct thing" should be "union thing".
              Age has not withered nor custom staled the correctness
              of your thought. Thanks.

              --
              Eric Sosman
              esosman@ieee-dot-org.invalid

              Comment

              • Serve Laurijssen

                #8
                Re: Unions in structures


                "Eric Sosman" <esosman@ieee-dot-org.invalidschr eef in bericht
                news:2LmdnVLdlK 3oC0DanZ2dnUVZ_ gmdnZ2d@comcast .com...
                Age has not withered nor custom staled the correctness
                of your thought. Thanks.
                I somehow get the urge to print this out in big letters and hang it on the
                wall. But eh what does it mean?

                (literally)


                Comment

                Working...