6.5.2.3 Structure and union members

Previous Table of Contents "New C Standard" commentary

1029 The first operand of the . operator shall have a qualified or unqualified structure or union type, and the second operand shall name a member of that type.

1030 The first operand of the -> operator shall have type “pointer to qualified or unqualified structure” or “pointer to qualified or unqualified union”, and the second operand shall name a member of the type pointed to.

1031 A postfix expression followed by the . operator and an identifier designates a member of a structure or union object.

1032 The value is that of the named member DR283), and is an lvalue if the first expression is an lvalue.

1033 If the first expression has qualified type, the result has the so-qualified version of the type of the designated member.

1034 A postfix expression followed by the -> operator and an identifier designates a member of a structure or union object.

1035 The value is that of the named member of the object to which the first expression points, and is an lvalue.80)

1036 If the first expression is a pointer to a qualified type, the result has the so-qualified version of the type of the designated member.

1037 One special guarantee is made in order to simplify the use of unions: if a union contains several structures that share a common initial sequence (see below), and if the union object currently contains one of these structures, it is permitted to inspect the common initial part of any of them anywhere that a declaration of the complete type of the union is visible.

1038 Two structures share a common initial sequence if corresponding members have compatible types (and, for bit-fields, the same widths) for a sequence of one or more initial members.

1039 EXAMPLE 1 If f is a function returning a structure or union, and x is a member of that structure or union, f().x is a valid postfix expression but is not an lvalue.

1040 EXAMPLE 2 In:


        struct s { int i; const int ci; };
        struct s s;
        const struct s cs;
        volatile struct s vs;

the various members have the types:

s.i         int
s.ci        const int
cs.i        const int
cs.ci       const int
vs.i        volatile int
vs.ci       volatile const int

1041 80) If &E is a valid pointer expression (where & is the “address-of” operator, which generates a pointer to its operand), the expression (&E)->MOS is the same as E.MOS.

1042 DR283) If the member used to access the contents of a union object is not the same as the member last used to store a value in the object, the appropriate part of the object representation of the value is reinterpreted as an object representation in the new type as described in 6.2.6 (a process sometimes called "type punning").

1043 This might be a trap representation.

1044 EXAMPLE 3 The following is a valid fragment:


        union {
                struct {
                        int    alltypes;
                } n;
                struct {
                        int    type;
                        int    intnode;
                } ni;
                struct {
                        int    type;
                        double doublenode;
                } nf;
        } u;
        u.nf.type = 1;
        u.nf.doublenode = 3.14;
        /* ... */
        if (u.n.alltypes == 1)
                if (sin(u.nf.doublenode) == 0.0)
                        /* ... */

The following is not a valid fragment (because the union type is not visible within function f):


        struct t1 { int m; };
        struct t2 { int m; };
        int f(struct t1 *p1, struct t2 *p2)
        {
                if (p1->m < 0)
                        p2->m = -p2->m;
                return p1->m;
        }
        int g()
        {
                union {
                        struct t1 s1;
                        struct t2 s2;
                } u;
                /* ... */
                return f(&u.s1, &u.s2);
        }

1045 Forward references: address and indirection operators (6.5.3.2), structure and union specifiers (6.7.2.1).

Next

Created at: 2008-01-30 02:39:42 The text from WG14/N1256 is copyright © ISO