| <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> |
| <html> |
| <!-- Copyright (C) 1988-2015 Free Software Foundation, Inc. |
| |
| Permission is granted to copy, distribute and/or modify this document |
| under the terms of the GNU Free Documentation License, Version 1.3 or |
| any later version published by the Free Software Foundation; with the |
| Invariant Sections being "Funding Free Software", the Front-Cover |
| Texts being (a) (see below), and with the Back-Cover Texts being (b) |
| (see below). A copy of the license is included in the section entitled |
| "GNU Free Documentation License". |
| |
| (a) The FSF's Front-Cover Text is: |
| |
| A GNU Manual |
| |
| (b) The FSF's Back-Cover Text is: |
| |
| You have freedom to copy and modify this GNU Manual, like GNU |
| software. Copies published by the Free Software Foundation raise |
| funds for GNU development. --> |
| <!-- Created by GNU Texinfo 5.2, http://www.gnu.org/software/texinfo/ --> |
| <head> |
| <title>Using the GNU Compiler Collection (GCC): Designated Inits</title> |
| |
| <meta name="description" content="Using the GNU Compiler Collection (GCC): Designated Inits"> |
| <meta name="keywords" content="Using the GNU Compiler Collection (GCC): Designated Inits"> |
| <meta name="resource-type" content="document"> |
| <meta name="distribution" content="global"> |
| <meta name="Generator" content="makeinfo"> |
| <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> |
| <link href="index.html#Top" rel="start" title="Top"> |
| <link href="Option-Index.html#Option-Index" rel="index" title="Option Index"> |
| <link href="index.html#SEC_Contents" rel="contents" title="Table of Contents"> |
| <link href="C-Extensions.html#C-Extensions" rel="up" title="C Extensions"> |
| <link href="Case-Ranges.html#Case-Ranges" rel="next" title="Case Ranges"> |
| <link href="Compound-Literals.html#Compound-Literals" rel="prev" title="Compound Literals"> |
| <style type="text/css"> |
| <!-- |
| a.summary-letter {text-decoration: none} |
| blockquote.smallquotation {font-size: smaller} |
| div.display {margin-left: 3.2em} |
| div.example {margin-left: 3.2em} |
| div.indentedblock {margin-left: 3.2em} |
| div.lisp {margin-left: 3.2em} |
| div.smalldisplay {margin-left: 3.2em} |
| div.smallexample {margin-left: 3.2em} |
| div.smallindentedblock {margin-left: 3.2em; font-size: smaller} |
| div.smalllisp {margin-left: 3.2em} |
| kbd {font-style:oblique} |
| pre.display {font-family: inherit} |
| pre.format {font-family: inherit} |
| pre.menu-comment {font-family: serif} |
| pre.menu-preformatted {font-family: serif} |
| pre.smalldisplay {font-family: inherit; font-size: smaller} |
| pre.smallexample {font-size: smaller} |
| pre.smallformat {font-family: inherit; font-size: smaller} |
| pre.smalllisp {font-size: smaller} |
| span.nocodebreak {white-space:nowrap} |
| span.nolinebreak {white-space:nowrap} |
| span.roman {font-family:serif; font-weight:normal} |
| span.sansserif {font-family:sans-serif; font-weight:normal} |
| ul.no-bullet {list-style: none} |
| --> |
| </style> |
| |
| |
| </head> |
| |
| <body lang="en" bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#800080" alink="#FF0000"> |
| <a name="Designated-Inits"></a> |
| <div class="header"> |
| <p> |
| Next: <a href="Case-Ranges.html#Case-Ranges" accesskey="n" rel="next">Case Ranges</a>, Previous: <a href="Compound-Literals.html#Compound-Literals" accesskey="p" rel="prev">Compound Literals</a>, Up: <a href="C-Extensions.html#C-Extensions" accesskey="u" rel="up">C Extensions</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Option-Index.html#Option-Index" title="Index" rel="index">Index</a>]</p> |
| </div> |
| <hr> |
| <a name="Designated-Initializers"></a> |
| <h3 class="section">6.26 Designated Initializers</h3> |
| <a name="index-initializers-with-labeled-elements"></a> |
| <a name="index-labeled-elements-in-initializers"></a> |
| <a name="index-case-labels-in-initializers"></a> |
| <a name="index-designated-initializers"></a> |
| |
| <p>Standard C90 requires the elements of an initializer to appear in a fixed |
| order, the same as the order of the elements in the array or structure |
| being initialized. |
| </p> |
| <p>In ISO C99 you can give the elements in any order, specifying the array |
| indices or structure field names they apply to, and GNU C allows this as |
| an extension in C90 mode as well. This extension is not |
| implemented in GNU C++. |
| </p> |
| <p>To specify an array index, write |
| ‘<samp>[<var>index</var>] =</samp>’ before the element value. For example, |
| </p> |
| <div class="smallexample"> |
| <pre class="smallexample">int a[6] = { [4] = 29, [2] = 15 }; |
| </pre></div> |
| |
| <p>is equivalent to |
| </p> |
| <div class="smallexample"> |
| <pre class="smallexample">int a[6] = { 0, 0, 15, 0, 29, 0 }; |
| </pre></div> |
| |
| <p>The index values must be constant expressions, even if the array being |
| initialized is automatic. |
| </p> |
| <p>An alternative syntax for this that has been obsolete since GCC 2.5 but |
| GCC still accepts is to write ‘<samp>[<var>index</var>]</samp>’ before the element |
| value, with no ‘<samp>=</samp>’. |
| </p> |
| <p>To initialize a range of elements to the same value, write |
| ‘<samp>[<var>first</var> ... <var>last</var>] = <var>value</var></samp>’. This is a GNU |
| extension. For example, |
| </p> |
| <div class="smallexample"> |
| <pre class="smallexample">int widths[] = { [0 ... 9] = 1, [10 ... 99] = 2, [100] = 3 }; |
| </pre></div> |
| |
| <p>If the value in it has side-effects, the side-effects happen only once, |
| not for each initialized field by the range initializer. |
| </p> |
| <p>Note that the length of the array is the highest value specified |
| plus one. |
| </p> |
| <p>In a structure initializer, specify the name of a field to initialize |
| with ‘<samp>.<var>fieldname</var> =</samp>’ before the element value. For example, |
| given the following structure, |
| </p> |
| <div class="smallexample"> |
| <pre class="smallexample">struct point { int x, y; }; |
| </pre></div> |
| |
| <p>the following initialization |
| </p> |
| <div class="smallexample"> |
| <pre class="smallexample">struct point p = { .y = yvalue, .x = xvalue }; |
| </pre></div> |
| |
| <p>is equivalent to |
| </p> |
| <div class="smallexample"> |
| <pre class="smallexample">struct point p = { xvalue, yvalue }; |
| </pre></div> |
| |
| <p>Another syntax that has the same meaning, obsolete since GCC 2.5, is |
| ‘<samp><var>fieldname</var>:</samp>’, as shown here: |
| </p> |
| <div class="smallexample"> |
| <pre class="smallexample">struct point p = { y: yvalue, x: xvalue }; |
| </pre></div> |
| |
| <p>Omitted field members are implicitly initialized the same as objects |
| that have static storage duration. |
| </p> |
| <a name="index-designators"></a> |
| <p>The ‘<samp>[<var>index</var>]</samp>’ or ‘<samp>.<var>fieldname</var></samp>’ is known as a |
| <em>designator</em>. You can also use a designator (or the obsolete colon |
| syntax) when initializing a union, to specify which element of the union |
| should be used. For example, |
| </p> |
| <div class="smallexample"> |
| <pre class="smallexample">union foo { int i; double d; }; |
| |
| union foo f = { .d = 4 }; |
| </pre></div> |
| |
| <p>converts 4 to a <code>double</code> to store it in the union using |
| the second element. By contrast, casting 4 to type <code>union foo</code> |
| stores it into the union as the integer <code>i</code>, since it is |
| an integer. (See <a href="Cast-to-Union.html#Cast-to-Union">Cast to Union</a>.) |
| </p> |
| <p>You can combine this technique of naming elements with ordinary C |
| initialization of successive elements. Each initializer element that |
| does not have a designator applies to the next consecutive element of the |
| array or structure. For example, |
| </p> |
| <div class="smallexample"> |
| <pre class="smallexample">int a[6] = { [1] = v1, v2, [4] = v4 }; |
| </pre></div> |
| |
| <p>is equivalent to |
| </p> |
| <div class="smallexample"> |
| <pre class="smallexample">int a[6] = { 0, v1, v2, 0, v4, 0 }; |
| </pre></div> |
| |
| <p>Labeling the elements of an array initializer is especially useful |
| when the indices are characters or belong to an <code>enum</code> type. |
| For example: |
| </p> |
| <div class="smallexample"> |
| <pre class="smallexample">int whitespace[256] |
| = { [' '] = 1, ['\t'] = 1, ['\h'] = 1, |
| ['\f'] = 1, ['\n'] = 1, ['\r'] = 1 }; |
| </pre></div> |
| |
| <a name="index-designator-lists"></a> |
| <p>You can also write a series of ‘<samp>.<var>fieldname</var></samp>’ and |
| ‘<samp>[<var>index</var>]</samp>’ designators before an ‘<samp>=</samp>’ to specify a |
| nested subobject to initialize; the list is taken relative to the |
| subobject corresponding to the closest surrounding brace pair. For |
| example, with the ‘<samp>struct point</samp>’ declaration above: |
| </p> |
| <div class="smallexample"> |
| <pre class="smallexample">struct point ptarray[10] = { [2].y = yv2, [2].x = xv2, [0].x = xv0 }; |
| </pre></div> |
| |
| <p>If the same field is initialized multiple times, it has the value from |
| the last initialization. If any such overridden initialization has |
| side-effect, it is unspecified whether the side-effect happens or not. |
| Currently, GCC discards them and issues a warning. |
| </p> |
| <hr> |
| <div class="header"> |
| <p> |
| Next: <a href="Case-Ranges.html#Case-Ranges" accesskey="n" rel="next">Case Ranges</a>, Previous: <a href="Compound-Literals.html#Compound-Literals" accesskey="p" rel="prev">Compound Literals</a>, Up: <a href="C-Extensions.html#C-Extensions" accesskey="u" rel="up">C Extensions</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Option-Index.html#Option-Index" title="Index" rel="index">Index</a>]</p> |
| </div> |
| |
| |
| |
| </body> |
| </html> |