Compound-Literals.html 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2. <html>
  3. <!-- Copyright (C) 1988-2017 Free Software Foundation, Inc.
  4. Permission is granted to copy, distribute and/or modify this document
  5. under the terms of the GNU Free Documentation License, Version 1.3 or
  6. any later version published by the Free Software Foundation; with the
  7. Invariant Sections being "Funding Free Software", the Front-Cover
  8. Texts being (a) (see below), and with the Back-Cover Texts being (b)
  9. (see below). A copy of the license is included in the section entitled
  10. "GNU Free Documentation License".
  11. (a) The FSF's Front-Cover Text is:
  12. A GNU Manual
  13. (b) The FSF's Back-Cover Text is:
  14. You have freedom to copy and modify this GNU Manual, like GNU
  15. software. Copies published by the Free Software Foundation raise
  16. funds for GNU development. -->
  17. <!-- Created by GNU Texinfo 5.2, http://www.gnu.org/software/texinfo/ -->
  18. <head>
  19. <title>Using the GNU Compiler Collection (GCC): Compound Literals</title>
  20. <meta name="description" content="Using the GNU Compiler Collection (GCC): Compound Literals">
  21. <meta name="keywords" content="Using the GNU Compiler Collection (GCC): Compound Literals">
  22. <meta name="resource-type" content="document">
  23. <meta name="distribution" content="global">
  24. <meta name="Generator" content="makeinfo">
  25. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  26. <link href="index.html#Top" rel="start" title="Top">
  27. <link href="Option-Index.html#Option-Index" rel="index" title="Option Index">
  28. <link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
  29. <link href="C-Extensions.html#C-Extensions" rel="up" title="C Extensions">
  30. <link href="Designated-Inits.html#Designated-Inits" rel="next" title="Designated Inits">
  31. <link href="Initializers.html#Initializers" rel="prev" title="Initializers">
  32. <style type="text/css">
  33. <!--
  34. a.summary-letter {text-decoration: none}
  35. blockquote.smallquotation {font-size: smaller}
  36. div.display {margin-left: 3.2em}
  37. div.example {margin-left: 3.2em}
  38. div.indentedblock {margin-left: 3.2em}
  39. div.lisp {margin-left: 3.2em}
  40. div.smalldisplay {margin-left: 3.2em}
  41. div.smallexample {margin-left: 3.2em}
  42. div.smallindentedblock {margin-left: 3.2em; font-size: smaller}
  43. div.smalllisp {margin-left: 3.2em}
  44. kbd {font-style:oblique}
  45. pre.display {font-family: inherit}
  46. pre.format {font-family: inherit}
  47. pre.menu-comment {font-family: serif}
  48. pre.menu-preformatted {font-family: serif}
  49. pre.smalldisplay {font-family: inherit; font-size: smaller}
  50. pre.smallexample {font-size: smaller}
  51. pre.smallformat {font-family: inherit; font-size: smaller}
  52. pre.smalllisp {font-size: smaller}
  53. span.nocodebreak {white-space:nowrap}
  54. span.nolinebreak {white-space:nowrap}
  55. span.roman {font-family:serif; font-weight:normal}
  56. span.sansserif {font-family:sans-serif; font-weight:normal}
  57. ul.no-bullet {list-style: none}
  58. -->
  59. </style>
  60. </head>
  61. <body lang="en" bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#800080" alink="#FF0000">
  62. <a name="Compound-Literals"></a>
  63. <div class="header">
  64. <p>
  65. Next: <a href="Designated-Inits.html#Designated-Inits" accesskey="n" rel="next">Designated Inits</a>, Previous: <a href="Initializers.html#Initializers" accesskey="p" rel="prev">Initializers</a>, Up: <a href="C-Extensions.html#C-Extensions" accesskey="u" rel="up">C Extensions</a> &nbsp; [<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>
  66. </div>
  67. <hr>
  68. <a name="Compound-Literals-1"></a>
  69. <h3 class="section">6.26 Compound Literals</h3>
  70. <a name="index-constructor-expressions"></a>
  71. <a name="index-initializations-in-expressions"></a>
  72. <a name="index-structures_002c-constructor-expression"></a>
  73. <a name="index-expressions_002c-constructor"></a>
  74. <a name="index-compound-literals"></a>
  75. <p>A compound literal looks like a cast of a brace-enclosed aggregate
  76. initializer list. Its value is an object of the type specified in
  77. the cast, containing the elements specified in the initializer.
  78. Unlike the result of a cast, a compound literal is an lvalue. ISO
  79. C99 and later support compound literals. As an extension, GCC
  80. supports compound literals also in C90 mode and in C++, although
  81. as explained below, the C++ semantics are somewhat different.
  82. </p>
  83. <p>Usually, the specified type of a compound literal is a structure. Assume
  84. that <code>struct foo</code> and <code>structure</code> are declared as shown:
  85. </p>
  86. <div class="smallexample">
  87. <pre class="smallexample">struct foo {int a; char b[2];} structure;
  88. </pre></div>
  89. <p>Here is an example of constructing a <code>struct foo</code> with a compound literal:
  90. </p>
  91. <div class="smallexample">
  92. <pre class="smallexample">structure = ((struct foo) {x + y, 'a', 0});
  93. </pre></div>
  94. <p>This is equivalent to writing the following:
  95. </p>
  96. <div class="smallexample">
  97. <pre class="smallexample">{
  98. struct foo temp = {x + y, 'a', 0};
  99. structure = temp;
  100. }
  101. </pre></div>
  102. <p>You can also construct an array, though this is dangerous in C++, as
  103. explained below. If all the elements of the compound literal are
  104. (made up of) simple constant expressions suitable for use in
  105. initializers of objects of static storage duration, then the compound
  106. literal can be coerced to a pointer to its first element and used in
  107. such an initializer, as shown here:
  108. </p>
  109. <div class="smallexample">
  110. <pre class="smallexample">char **foo = (char *[]) { &quot;x&quot;, &quot;y&quot;, &quot;z&quot; };
  111. </pre></div>
  112. <p>Compound literals for scalar types and union types are also allowed. In
  113. the following example the variable <code>i</code> is initialized to the value
  114. <code>2</code>, the result of incrementing the unnamed object created by
  115. the compound literal.
  116. </p>
  117. <div class="smallexample">
  118. <pre class="smallexample">int i = ++(int) { 1 };
  119. </pre></div>
  120. <p>As a GNU extension, GCC allows initialization of objects with static storage
  121. duration by compound literals (which is not possible in ISO C99 because
  122. the initializer is not a constant).
  123. It is handled as if the object were initialized only with the brace-enclosed
  124. list if the types of the compound literal and the object match.
  125. The elements of the compound literal must be constant.
  126. If the object being initialized has array type of unknown size, the size is
  127. determined by the size of the compound literal.
  128. </p>
  129. <div class="smallexample">
  130. <pre class="smallexample">static struct foo x = (struct foo) {1, 'a', 'b'};
  131. static int y[] = (int []) {1, 2, 3};
  132. static int z[] = (int [3]) {1};
  133. </pre></div>
  134. <p>The above lines are equivalent to the following:
  135. </p><div class="smallexample">
  136. <pre class="smallexample">static struct foo x = {1, 'a', 'b'};
  137. static int y[] = {1, 2, 3};
  138. static int z[] = {1, 0, 0};
  139. </pre></div>
  140. <p>In C, a compound literal designates an unnamed object with static or
  141. automatic storage duration. In C++, a compound literal designates a
  142. temporary object that only lives until the end of its full-expression.
  143. As a result, well-defined C code that takes the address of a subobject
  144. of a compound literal can be undefined in C++, so G++ rejects
  145. the conversion of a temporary array to a pointer. For instance, if
  146. the array compound literal example above appeared inside a function,
  147. any subsequent use of <code>foo</code> in C++ would have undefined behavior
  148. because the lifetime of the array ends after the declaration of <code>foo</code>.
  149. </p>
  150. <p>As an optimization, G++ sometimes gives array compound literals longer
  151. lifetimes: when the array either appears outside a function or has
  152. a <code>const</code>-qualified type. If <code>foo</code> and its initializer had
  153. elements of type <code>char *const</code> rather than <code>char *</code>, or if
  154. <code>foo</code> were a global variable, the array would have static storage
  155. duration. But it is probably safest just to avoid the use of array
  156. compound literals in C++ code.
  157. </p>
  158. <hr>
  159. <div class="header">
  160. <p>
  161. Next: <a href="Designated-Inits.html#Designated-Inits" accesskey="n" rel="next">Designated Inits</a>, Previous: <a href="Initializers.html#Initializers" accesskey="p" rel="prev">Initializers</a>, Up: <a href="C-Extensions.html#C-Extensions" accesskey="u" rel="up">C Extensions</a> &nbsp; [<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>
  162. </div>
  163. </body>
  164. </html>