You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

184 satır
8.1KB

  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-2020 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 6.5, http://www.gnu.org/software/texinfo/ -->
  18. <head>
  19. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  20. <title>Compound Literals (Using the GNU Compiler Collection (GCC))</title>
  21. <meta name="description" content="Compound Literals (Using the GNU Compiler Collection (GCC))">
  22. <meta name="keywords" content="Compound Literals (Using the GNU Compiler Collection (GCC))">
  23. <meta name="resource-type" content="document">
  24. <meta name="distribution" content="global">
  25. <meta name="Generator" content="makeinfo">
  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.indentedblock {margin-right: 0em}
  36. blockquote.smallindentedblock {margin-right: 0em; font-size: smaller}
  37. blockquote.smallquotation {font-size: smaller}
  38. div.display {margin-left: 3.2em}
  39. div.example {margin-left: 3.2em}
  40. div.lisp {margin-left: 3.2em}
  41. div.smalldisplay {margin-left: 3.2em}
  42. div.smallexample {margin-left: 3.2em}
  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.nolinebreak {white-space: nowrap}
  54. span.roman {font-family: initial; font-weight: normal}
  55. span.sansserif {font-family: sans-serif; font-weight: normal}
  56. ul.no-bullet {list-style: none}
  57. -->
  58. </style>
  59. </head>
  60. <body lang="en">
  61. <a name="Compound-Literals"></a>
  62. <div class="header">
  63. <p>
  64. 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>
  65. </div>
  66. <hr>
  67. <a name="Compound-Literals-1"></a>
  68. <h3 class="section">6.28 Compound Literals</h3>
  69. <a name="index-constructor-expressions"></a>
  70. <a name="index-initializations-in-expressions"></a>
  71. <a name="index-structures_002c-constructor-expression"></a>
  72. <a name="index-expressions_002c-constructor"></a>
  73. <a name="index-compound-literals"></a>
  74. <p>A compound literal looks like a cast of a brace-enclosed aggregate
  75. initializer list. Its value is an object of the type specified in
  76. the cast, containing the elements specified in the initializer.
  77. Unlike the result of a cast, a compound literal is an lvalue. ISO
  78. C99 and later support compound literals. As an extension, GCC
  79. supports compound literals also in C90 mode and in C++, although
  80. as explained below, the C++ semantics are somewhat different.
  81. </p>
  82. <p>Usually, the specified type of a compound literal is a structure. Assume
  83. that <code>struct foo</code> and <code>structure</code> are declared as shown:
  84. </p>
  85. <div class="smallexample">
  86. <pre class="smallexample">struct foo {int a; char b[2];} structure;
  87. </pre></div>
  88. <p>Here is an example of constructing a <code>struct foo</code> with a compound literal:
  89. </p>
  90. <div class="smallexample">
  91. <pre class="smallexample">structure = ((struct foo) {x + y, 'a', 0});
  92. </pre></div>
  93. <p>This is equivalent to writing the following:
  94. </p>
  95. <div class="smallexample">
  96. <pre class="smallexample">{
  97. struct foo temp = {x + y, 'a', 0};
  98. structure = temp;
  99. }
  100. </pre></div>
  101. <p>You can also construct an array, though this is dangerous in C++, as
  102. explained below. If all the elements of the compound literal are
  103. (made up of) simple constant expressions suitable for use in
  104. initializers of objects of static storage duration, then the compound
  105. literal can be coerced to a pointer to its first element and used in
  106. such an initializer, as shown here:
  107. </p>
  108. <div class="smallexample">
  109. <pre class="smallexample">char **foo = (char *[]) { &quot;x&quot;, &quot;y&quot;, &quot;z&quot; };
  110. </pre></div>
  111. <p>Compound literals for scalar types and union types are also allowed. In
  112. the following example the variable <code>i</code> is initialized to the value
  113. <code>2</code>, the result of incrementing the unnamed object created by
  114. the compound literal.
  115. </p>
  116. <div class="smallexample">
  117. <pre class="smallexample">int i = ++(int) { 1 };
  118. </pre></div>
  119. <p>As a GNU extension, GCC allows initialization of objects with static storage
  120. duration by compound literals (which is not possible in ISO C99 because
  121. the initializer is not a constant).
  122. It is handled as if the object were initialized only with the brace-enclosed
  123. list if the types of the compound literal and the object match.
  124. The elements of the compound literal must be constant.
  125. If the object being initialized has array type of unknown size, the size is
  126. determined by the size of the compound literal.
  127. </p>
  128. <div class="smallexample">
  129. <pre class="smallexample">static struct foo x = (struct foo) {1, 'a', 'b'};
  130. static int y[] = (int []) {1, 2, 3};
  131. static int z[] = (int [3]) {1};
  132. </pre></div>
  133. <p>The above lines are equivalent to the following:
  134. </p><div class="smallexample">
  135. <pre class="smallexample">static struct foo x = {1, 'a', 'b'};
  136. static int y[] = {1, 2, 3};
  137. static int z[] = {1, 0, 0};
  138. </pre></div>
  139. <p>In C, a compound literal designates an unnamed object with static or
  140. automatic storage duration. In C++, a compound literal designates a
  141. temporary object that only lives until the end of its full-expression.
  142. As a result, well-defined C code that takes the address of a subobject
  143. of a compound literal can be undefined in C++, so G++ rejects
  144. the conversion of a temporary array to a pointer. For instance, if
  145. the array compound literal example above appeared inside a function,
  146. any subsequent use of <code>foo</code> in C++ would have undefined behavior
  147. because the lifetime of the array ends after the declaration of <code>foo</code>.
  148. </p>
  149. <p>As an optimization, G++ sometimes gives array compound literals longer
  150. lifetimes: when the array either appears outside a function or has
  151. a <code>const</code>-qualified type. If <code>foo</code> and its initializer had
  152. elements of type <code>char *const</code> rather than <code>char *</code>, or if
  153. <code>foo</code> were a global variable, the array would have static storage
  154. duration. But it is probably safest just to avoid the use of array
  155. compound literals in C++ code.
  156. </p>
  157. <hr>
  158. <div class="header">
  159. <p>
  160. 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>
  161. </div>
  162. </body>
  163. </html>