HPlogo HP C/HP-UX Reference Manual: Version A.05.55.02 > Chapter 3 Data Types and Declarations

Compound Literal

» 

Technical documentation

Complete book in PDF

 » Table of Contents

 » Index

Compound literal provide a mechanism for specifying constants of aggregate or union type. Compound literal is a part of the C99 standards (ISO/IEC 9899:1999: 6.5.2.5). Compound literals are an easy means of initializing an object of type aggregate or union without having to allocate a temporary variable for the object. It is represented as an unnamed object with a type and has an lvalue.

Syntax

(type-name) {initializer-list}

type-name must specify an object type or an array of unknown size. The value of the compound literal is that of an unnamed object initialized by the initializer list. The object has static storage if the compound literal occurs outside the body of the function, otherwise it has automatic storage duration associated with the enclosing blocks.

Examples

The following examples detail the usage of compound literals:

Example 3-1 An Array of Scalars

int *p=(int[]) {1,2};

In this example, an array of size 2 has been declared, with the first two elements initialized to 1 and 2. (int [ ]){1,2} represents the compound literal and it is assigned to a pointer variable p of type int.

Example 3-2 An Array of Structures

struct node
{
   int a;
   int b;
};
struct node *st=(struct node[2]) {1,2,3,4};

In this example, a pointer of structures has been initialized with the unnamed compund literal object. (struct node[2]){1,2,3,4} is the compound literal and is converted to struct node * and assigned to st.

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 defining 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 */ }

This example is for structure objects created using compound literals, which are passed to functions.

Example 3-9 Example 9: Possible Ways of Using Compound Literals

int x = (int){5};              // initializing x with 5
int *y = (int *){&x};            // initializing y with address of x

The purpose of the above example is only to show the other possible ways of using compound literals.

NOTE: Compound literal is recognized only in the C99 mode (-AC99).
© Hewlett-Packard Development Company, L.P.