About C/C ++'s {0} initializer

Source: Internet
Author: User

The name is written around {0}, which is commonly used in struct initializer writing. It is always so useful. Today someone else asked me. The C/C ++ standard is missing. I finally figured out the problem. Take the C99 standard as the standard -- the C ++ standard is copied from the C standard.
Obviously, there are rules for incomplete initializer list actions in the standard, and the relevant statements are as follows (the following quotes are all cited from the C99 standard draft ):
[6.7.8.21] If there are fewer initializers in a brace-enclosed list than there are elements or members of an aggregate, or fewer characters in a string literal used to initialize an array of known size than there are elements in the array, the remainder of the aggregate shall be initialized implicitly the same as objects that have static storage duration.
That is to say, like this:
Struct {int a; int B ;}aa = {0 };
The statement is completely correct, and a and B are all zeroing (a is from initializer literal, and B is described above ).
The basic knowledge is that all non-composite types in C (I made a word, so I don't want to elaborate on it) can be initialized with 0, therefore, it is okay to initialize the similar structure of {0} in the above example. Some people may wonder what to do if the first element of struct is struct. The Standard specifies the behavior in this case:
[6.7.8.20] If the aggregate contains elements or members that are aggregates or unions, or if the first member of a union is an aggregate or union, these rules apply recursively to the subaggregates or contained unions. if the initializer of a subaggregate or contained union begins with a left brace, the initializers enclosed by that brace and its matching right brace initialize the elements or members of the subaggregate or the first member of the contained union. otherwise, only enough initializers from the list are taken to account for the elements or members of the subaggregate or the first member of the contained union; any remaining initializers are left to initialize the next element or member of the aggregate of which the current subaggregate or contained union is a part.
This section is very long. To put it simply, if an element of aggregate (which is simply understood as struct, an accurate expression to see the Standard) is aggregate, according to the general aggregate initialization rules, find the place where the initialization list of the aggregate starts. If it is expanded in braces, use the list in the Ampersand to initialize the aggregate, otherwise, we will start to find "enough" entries for ourselves to initialize, and the rest will be used by others.
In this example:
Struct {int a; int B;} aa; int c;} _ a = {1, 2 };
The first element of struct is struct, and initializer has no {...} In this case, it is a "1", and it will take the "enough" initializer from here to initialize itself, that is, it will use "1, 2" to initialize a and B. So the result is a =={ 1, 2}, 0 };
In this way, it is easy to understand the above structure with {0} initialization.

Org version 7.8.05 with Emacs version 23

 


From the smile of Kalin

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.