Note that there are some explanatory texts on larger screens.

plurals
  1. POCommon initial sequence in structures nested within union - definition in C standard
    text
    copied!<p>In C11 standard there is following definition of common initial sequence shared by structures nested within a single union:</p> <blockquote> <p><strong>6.5.2.3/6</strong> </p> <p>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 completed type of the union is visible. Two structures share a <em>common initial sequence</em> if corresponding members have compatible types (and, for bit-fields, the same widths) for a sequence of one or more initial members.</p> <p>EXAMPLE 3 The following is a valid fragment:</p> <pre><code>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) /* ... */ </code></pre> </blockquote> <p>According to my understanding of this article, the above code, however, is invalid.</p> <p>In the outer <code>if</code> statement we indicate that <code>n::alltypes</code> data member is active (simultaneously with <code>ni::type</code> and <code>nf::type</code> as the standard states) yet in the inner <code>if</code> we use <code>nf::doublenode</code> which is not a part of the common initial sequence.</p> <p>Can somebody clarify this issue?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload