HP C A.06.05 Reference Manual
Data Types and Declarations
Compound Literal
Chapter 368
Example 3-3 As a Parameter
int main()
{
foo((int [])(1,2,3,4));
}
int foo(int *p)
{
}
In this example, a compound literal is passed as a parameter to function foo() instead of
creating a temporary variable in the function main() and then passing the compound literal
as a parameter to foo(). Compound literals can be passed as parameters to functions
eliminating the need of deļ¬ning a temporary variable in the caller function.
Example 3-4 Element in an Array
int *p=(int [10000]){[999]=20};
This example shows how a particular element[999] in an array of size 10000 can be
initialized explicitly.
Example 3-5 An Array of Characters
char *c=(char []){"/tmp/testfile"};
This example shows how a compound literal is used to initialize an array of characters.
Example 3-6 Constant Compound Literal
(const float []) {1e0,1e1};
This example shows a constant compound literal.
Example 3-7 Single Compound Literal
struct int_list {
int car;
struct int_list *cdr;
};
struct int_list endless_zeros = {0. &endless_zeros};
This example shows how a single compound literal cannot be used to specify a circularly
linked object, since compound literals are unnamed.
Example 3-8 Structure Objects
drawline((struct point){1,1},&(struct point){3,3}); /* call */
drawline(struct point, struct point *) { /* definition */ }