Thread: What is a structure tag?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    The idea is that a structure definition can either a) declare a new type of structure, or b) declare instance variables of a structure, or c) both. If you only want to use the structure once, you don't need to give it a name (tag).

    In addition to the example laserlight gave, in C you might see a structure without a tag in the case of nested structures... for example,
    Code:
    struct widget {
        int x, y;
        struct {
            void *(*clone)(struct widget *);  // function pointers to clone the widget
            void (*free)(struct widget *);  // and free the widget
        } func;
    };
    
    widget w;
    w.func.free = &free_widget;
    The inner structure separates its members from the outer structure without creating a structure that you can refer to by name. This is more common with unions than structures. (There are also anonymous unions that have neither a name nor instances, but that's a very special case.)
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  2. #2
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    Quote Originally Posted by dwks View Post
    ...

    The inner structure separates its members from the outer structure without creating a structure that you can refer to by name. This is more common with unions than structures. (There are also anonymous unions that have neither a name nor instances, but that's a very special case.)
    Yeah and it's boss for representing coordinates!

    Code:
    struct point
    {
        // Use a union as hybrid storage
    
    
        union
        {
            struct
            {
                float x, y, z;
            };
    
    
            float p[3];
        };
    
    
        point(float a, float b, float c) : x(a), y(b), z(c) { };
    
    
        void print(void) const
        {
            printf("(%.00f, %.00f, %.00f)\n", x, y, z);
        };
    };

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by MutantJohn View Post
    Yeah and it's boss for representing coordinates!

    Code:
    struct point
    {
        // Use a union as hybrid storage
    
    
        union
        {
            struct
            {
                float x, y, z;
            };
    
    
            float p[3];
        };
    
    
        point(float a, float b, float c) : x(a), y(b), z(c) { };
    
    
        void print(void) const
        {
            printf("(%.00f, %.00f, %.00f)\n", x, y, z);
        };
    };
    Is this safe according to the standard?
    Note: I would guess packing might need to be done exactly right or disabled.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by stahta01 View Post
    Is this safe according to the standard?
    Note: I would guess packing might need to be done exactly right or disabled.
    Depends what you mean by safe.

    Your code, as presented, only sets the values of struct members x,y, and z and only ever retrieves values of them. There is no usage of the member p. As such, the behaviour is well specified.

    The behaviour would be undefined if one member of the union is set and another member is used to retrieve (e.g. setting x,y,z but retrieving p[0],p[1], and p[2]).

    That said, a fair few library functions would rely on the layout aligning. But "what implementations do" is not the same as "what standards require".
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  5. #5
    Registered User
    Join Date
    Jul 2014
    Location
    Central Arizona
    Posts
    61
    Thank You all for enlightening me. You've provides some interesting examples of when a struct or union don't need to have a name tag. However, after thinking about it for a while, its probably a good practice to supply a name anyway.

  6. #6
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    Quote Originally Posted by grumpy View Post
    Depends what you mean by safe.

    Your code, as presented, only sets the values of struct members x,y, and z and only ever retrieves values of them. There is no usage of the member p. As such, the behaviour is well specified.

    The behaviour would be undefined if one member of the union is set and another member is used to retrieve (e.g. setting x,y,z but retrieving p[0],p[1], and p[2]).

    That said, a fair few library functions would rely on the layout aligning. But "what implementations do" is not the same as "what standards require".
    What do you mean by layout aligning?

    Do you mean it's implementation defined that x, y and z are contiguous and in-order in memory? That some compilers would read the struct x, y and z and store them in any order?

    Also and with regards to the union, does the array force x, y and z to be contiguous? The array must be contiguous by definition so does this force x, y and z into also being that way because the same data is shared for all members of the union.

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by MutantJohn View Post
    What do you mean by layout aligning?
    Look up structure padding.

    Placing a struct and an array in the same union does not force the struct members to align with array elements.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 10
    Last Post: 03-18-2014, 10:43 AM
  2. Replies: 4
    Last Post: 04-25-2010, 10:57 AM
  3. Replies: 1
    Last Post: 04-02-2009, 06:51 AM
  4. Replies: 9
    Last Post: 05-21-2007, 12:10 AM
  5. Replies: 4
    Last Post: 11-22-2006, 12:20 PM