Use of compound literal in C language

Source: Internet
Author: User

C99 increases the feature, compound literal (composite literal). Once you are familiar with and use it, you will experience a simple and powerful expression.

The so-called literal is the representation of a fixed value. Both numeric and string types have literal representations. Such as:

1.23f, "literral" are literal int    x = 100;float  y = 1.23f;char* s = "literral";

You can see the literal build and copy it to the variable. Since it is a fixed value, it can be built at initialization time.



So compound literal is a compound of several meanings?

Composite in addition to the compound meaning, there is also a combination of concepts. Since the combination, the inevitable array is the intuitive counterpart.

So, the compound literal is the meaning of the array literal.

Once, how did we define the array.

int arr1[]   = {0, 1, 2, 3};int arr2[10] = {0};

This is the simplest array, and {} is also a literal definition. However, this literal can only be used when the array is initialized, and cannot be assigned a value operation.

int x;//correct x = 100;int arr[1];//Error arr = {0};


Yes, the compound literal is the array literal that can be defined at any time and assigned to the value. Two-step build-up.

1. Shape (type[]) denotes the type of array to be constructed, such as: (Int[]), (int*p), and can be a custom type (mytype[])

2. Follow {} to indicate array contents

int*  intArr1 = (int[]) {0, 1, 2};int*  intArr2 = (int[100]) {0};intarr1  = (int[])  {3, 4, 5};intarr2  = ( Int[1]) {1};typedef struct {    void* data;       int     length;} Array; array*  arr1 = (array[])  {NULL, 0}; array*  arr2 = (array[1]) {(int[]) {1}, 1};

Visible, compound literal, returns a pointer to the constructed array literal.

Can be defined at any time, an array of literal pointers, can be passed parameters, initialization structure, more concise.



As an example:

typedef struct {void* data;int   length;} array;void foo (array* arr) {}//used to notation int   data[]  = {1}; Array arr [1] = {data, 1};foo (arr);//Current Notation Foo ((array[]) {(int[]) {1}, 1});

The expression is more concise, also saves a lot of assignment operation. Many times, the construction of the literal array is just for the parameters of a function call.



static array* arr = (array[]) {(int[10]) {0},1};
This example illustrates the use of initializing a struct. Any complex custom type, we are able to define the literal value of the initialization through compound literals.

This is useful in building some fixed data structures that need to be initialized.



Also, compound text can be left-valued. The value can be assigned whether the literal value is created on the stack or on the heap.

This is great, you can imagine, there are more dark magic to be explored.




Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Use of compound literal in C language

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.