29.10.2014 Views

ARM Compiler toolchain v4.1 for µVision Using the Compiler

ARM Compiler toolchain v4.1 for µVision Using the Compiler

ARM Compiler toolchain v4.1 for µVision Using the Compiler

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>Compiler</strong> Coding Practices<br />

5.63 Compound literals in C99<br />

ISO C99 supports compound literals. A compound literal looks like a cast followed by an<br />

initializer. Its value is an object of <strong>the</strong> type specified in <strong>the</strong> cast, containing <strong>the</strong> elements<br />

specified in <strong>the</strong> initializer. It is an lvalue. For example:<br />

int *y = (int []) {1, 2, 3};<br />

int *z = (int [3]) {1};<br />

Note<br />

int *y = (int []) {1, 2, 3}; is accepted by <strong>the</strong> compiler, but int y[] = (int []) {1, 2, 3};<br />

is not accepted as a high-level (global) initialization.<br />

In <strong>the</strong> following example source code, <strong>the</strong> compound literals are:<br />

• (struct T) { 43, "world"}<br />

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

• &(struct T) {43, "hello"}<br />

• (int[]){1, 2, 3}<br />

struct T<br />

{<br />

int a;<br />

char *b;<br />

} t2;<br />

void g(const struct T *t);<br />

void f()<br />

{<br />

int x[10];<br />

...<br />

}<br />

t2 = (struct T) {43, "world"};<br />

g(&(struct T) {.b = "hello", .a = 47});<br />

g(&(struct T) {43, "bye"});<br />

memcpy(x, (int[]){1, 2, 3}, 3 * sizeof(int));<br />

5.63.1 See also<br />

Concepts<br />

• New language features of C99 on page 5-77.<br />

<strong>ARM</strong> DUI 0375C Copyright © 2007-2008, 2011 <strong>ARM</strong>. All rights reserved. 5-81<br />

ID061811<br />

Non-Confidential

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!