• Home
  • History
  • Annotate
  • Line#
  • Navigate
  • Raw
  • Download
  • only in /asuswrt-rt-n18u-9.0.0.4.380.2695/release/src-rt-6.x.4708/toolchains/hndtools-armeabi-2011.09/share/doc/arm-arm-none-eabi/html/gcc/
1<html lang="en">
2<head>
3<title>Compound Literals - Using the GNU Compiler Collection (GCC)</title>
4<meta http-equiv="Content-Type" content="text/html">
5<meta name="description" content="Using the GNU Compiler Collection (GCC)">
6<meta name="generator" content="makeinfo 4.13">
7<link title="Top" rel="start" href="index.html#Top">
8<link rel="up" href="C-Extensions.html#C-Extensions" title="C Extensions">
9<link rel="prev" href="Initializers.html#Initializers" title="Initializers">
10<link rel="next" href="Designated-Inits.html#Designated-Inits" title="Designated Inits">
11<link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage">
12<!--
13Copyright (C) 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1997,
141998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009,
152010 Free Software Foundation, Inc.
16
17Permission is granted to copy, distribute and/or modify this document
18under the terms of the GNU Free Documentation License, Version 1.3 or
19any later version published by the Free Software Foundation; with the
20Invariant Sections being ``Funding Free Software'', the Front-Cover
21Texts being (a) (see below), and with the Back-Cover Texts being (b)
22(see below).  A copy of the license is included in the section entitled
23``GNU Free Documentation License''.
24
25(a) The FSF's Front-Cover Text is:
26
27     A GNU Manual
28
29(b) The FSF's Back-Cover Text is:
30
31     You have freedom to copy and modify this GNU Manual, like GNU
32     software.  Copies published by the Free Software Foundation raise
33     funds for GNU development.-->
34<meta http-equiv="Content-Style-Type" content="text/css">
35<style type="text/css"><!--
36  pre.display { font-family:inherit }
37  pre.format  { font-family:inherit }
38  pre.smalldisplay { font-family:inherit; font-size:smaller }
39  pre.smallformat  { font-family:inherit; font-size:smaller }
40  pre.smallexample { font-size:smaller }
41  pre.smalllisp    { font-size:smaller }
42  span.sc    { font-variant:small-caps }
43  span.roman { font-family:serif; font-weight:normal; } 
44  span.sansserif { font-family:sans-serif; font-weight:normal; } 
45--></style>
46<link rel="stylesheet" type="text/css" href="../cs.css">
47</head>
48<body>
49<div class="node">
50<a name="Compound-Literals"></a>
51<p>
52Next:&nbsp;<a rel="next" accesskey="n" href="Designated-Inits.html#Designated-Inits">Designated Inits</a>,
53Previous:&nbsp;<a rel="previous" accesskey="p" href="Initializers.html#Initializers">Initializers</a>,
54Up:&nbsp;<a rel="up" accesskey="u" href="C-Extensions.html#C-Extensions">C Extensions</a>
55<hr>
56</div>
57
58<h3 class="section">6.25 Compound Literals</h3>
59
60<p><a name="index-constructor-expressions-2374"></a><a name="index-initializations-in-expressions-2375"></a><a name="index-structures_002c-constructor-expression-2376"></a><a name="index-expressions_002c-constructor-2377"></a><a name="index-compound-literals-2378"></a><!-- The GNU C name for what C99 calls compound literals was "constructor expressions". -->
61
62 <p>ISO C99 supports compound literals.  A compound literal looks like
63a cast containing an initializer.  Its value is an object of the
64type specified in the cast, containing the elements specified in
65the initializer; it is an lvalue.  As an extension, GCC supports
66compound literals in C90 mode and in C++.
67
68 <p>Usually, the specified type is a structure.  Assume that
69<code>struct foo</code> and <code>structure</code> are declared as shown:
70
71<pre class="smallexample">     struct foo {int a; char b[2];} structure;
72</pre>
73 <p class="noindent">Here is an example of constructing a <code>struct foo</code> with a compound literal:
74
75<pre class="smallexample">     structure = ((struct foo) {x + y, 'a', 0});
76</pre>
77 <p class="noindent">This is equivalent to writing the following:
78
79<pre class="smallexample">     {
80       struct foo temp = {x + y, 'a', 0};
81       structure = temp;
82     }
83</pre>
84 <p>You can also construct an array.  If all the elements of the compound literal
85are (made up of) simple constant expressions, suitable for use in
86initializers of objects of static storage duration, then the compound
87literal can be coerced to a pointer to its first element and used in
88such an initializer, as shown here:
89
90<pre class="smallexample">     char **foo = (char *[]) { "x", "y", "z" };
91</pre>
92 <p>Compound literals for scalar types and union types are is
93also allowed, but then the compound literal is equivalent
94to a cast.
95
96 <p>As a GNU extension, GCC allows initialization of objects with static storage
97duration by compound literals (which is not possible in ISO C99, because
98the initializer is not a constant). 
99It is handled as if the object was initialized only with the bracket
100enclosed list if the types of the compound literal and the object match. 
101The initializer list of the compound literal must be constant. 
102If the object being initialized has array type of unknown size, the size is
103determined by compound literal size.
104
105<pre class="smallexample">     static struct foo x = (struct foo) {1, 'a', 'b'};
106     static int y[] = (int []) {1, 2, 3};
107     static int z[] = (int [3]) {1};
108</pre>
109 <p class="noindent">The above lines are equivalent to the following:
110<pre class="smallexample">     static struct foo x = {1, 'a', 'b'};
111     static int y[] = {1, 2, 3};
112     static int z[] = {1, 0, 0};
113</pre>
114 </body></html>
115
116