5.66 Compound literals in C99

ISO C99 supports compound literals. A compound literal looks like a cast followed by an initializer.

Its value is an object of the type specified in the cast, containing the elements specified in the initializer. It is an lvalue.

For example:

int *y = (int []) {1, 2, 3}; 
int *z = (int [3]) {1};

Note

int *y = (int []) {1, 2, 3}; is accepted by the compiler, but int y[] = (int []) {1, 2, 3}; is not accepted as a high-level (global) initialization.

In the following example source code, the compound literals are:

  • (struct T) { 43, "world"}

  • &(struct T) {.b = "hello", .a = 47}

  • &(struct T) {43, "hello"}

  • (int[]){1, 2, 3}

struct T
{
   int a;
   char *b;
} t2;
void g(const struct T *t);
void f()
{
   int x[10];
   ...
   t2 = (struct T) {43, "world"};
   g(&(struct T) {.b = "hello", .a = 47});
   g(&(struct T) {43, "bye"});
   memcpy(x, (int[]){1, 2, 3}, 3 * sizeof(int));
}
Related concepts
5.63 New language features of C99
5.65 // comments in C99 and C90
5.67 Designated initializers in C99
5.68 Hexadecimal floating-point numbers in C99
5.69 Flexible array members in C99
5.70 __func__ predefined identifier in C99
5.71 inline functions in C99
5.72 long long data type in C99 and C90
5.73 Macros with a variable number of arguments in C99
5.74 Mixed declarations and statements in C99
5.75 New block scopes for selection and iteration statements in C99
5.76 _Pragma preprocessing operator in C99
5.77 Restricted pointers in C99
5.79 Complex numbers in C99
Non-ConfidentialPDF file icon PDF versionARM DUI0472J
Copyright © 2010-2013 ARM. All rights reserved.