Use of compound literal in C language

Source: Internet
Author: User

C99 adds an attribute, 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 that you can copy it to a variable at the same time as the literal construct. 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 a compound meaning. There are also combinations of concepts.

Since the combination. The bound array is intuitive and corresponding.


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

before. How we define the array.

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

This is the simplest array. {} is also a literal definition. However. Such a 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};


Right. The compound literal is the array literal that can be assigned at any time, at any time. Two-step build-up.

1. Form (type[]) indicates the type of array to be constructed, such as (int[]), (int*p), and the 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, which returns a pointer to the constructed array literal.

You can define a literal pointer to an array at any time. You can let the parameters pass. Initializes the structure. More concise.



To give a sample example:

typedef struct {void* data;int   length;} array;void foo (array* arr) {}//previous notation int   data[]  = {1}; Array arr [1] = {data, 1};foo (arr);//Today's notation Foo ((array[]) {(int[]) {1}, 1});

The expression is more concise. It also eliminates a lot of assignment operations. Very often, the construction of the literal array is passed only for a function call's number of parameters.



static array* arr = (array[]) {(int[10]) {0},1};
This sample illustrates the case. How to use a struct when initializing it.

Whatever a complex self-defined type. We can all define the literal value of the initialization by the compound literal.

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



And also. Compound text can be left-valued.

Whether the literal value is created on the stack or on the heap, it can be assigned.

This is great, can play the imagination, there are many other dark magic to be explored.




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.