Use of C-language compound literal

Source: Internet
Author: User

Use of C-language compound literal

The added feature of C99, composite literal ). Once familiar with and used, you will experience concise and powerful expressions.

A literal is a representation of a fixed value. Both numeric and string types have literal expressions. For example:

 

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

We can see that the literal build can be copied to the variable for use at the same time. Since it is a fixed value, it can be constructed during initialization.

 

 

 

So what are the meanings of compound literal?

In addition to the meaning of compound, composite also has the concept of combination. Since the combination, arrays must be intuitively matched.

Therefore, compound literal is the meaning of array literal.

We used to define arrays.

 

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

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

 

 

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 values at any time. Built in two steps.

1. (type []) indicates the type of the array to be constructed, such as (int []), (int * p), or a custom type (MyType []).

2. Follow {} to indicate the array content

 

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};

It can be seen that the compound literal volume returns a pointer pointing to the constructed array literal volume.

 

A literal pointer of an array can be defined at any time to facilitate parameter transmission and initialization.

 

 

For example:

 

Typedef struct {void * data; int length;} Array; void foo (Array * arr) {}// int data [] = {1 }; array arr [1] = {data, 1}; foo (arr); // The current foo (Array []) {(int []) {1 }, 1 });

The expression is more concise and saves a lot of value assignment operations. Most of the time, building a literal array is only for passing parameters in callback function calls.

 

 

 

 

static Array* arr = (Array[]) {(int[10]) {0},1};
This example illustrates how to initialize a structure. For any complicated custom type, we can use composite text to define the initial literal value.

 

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

 

 

In addition, compound text can be left-side. Values can be assigned no matter whether they are created on the stack or on the stack.

This is amazing. As you can imagine, there are more black magic to be explored.

 

 


 

Related Article

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.