Website: http://blog.chinaunix.net/uid-24807808-id-3219820.html
When you look at the Linux source, you will often see code similar to the following structure assignment:
- struct Device My_dev =
- {
- . Bus = &my_bus_type,
- . Parent = &my_bus,
- . Release = My_dev_release,
- };
On the whole, it seems that we usually meet the structure of the assignment of the same value, but in front of the variable added a point, as if we do not know what the meaning of.
The above assignment becomes the specified initialization (designated initializer). Derived from the ISO C99 standard.
C Primer Plus has a more detailed description, as follows:
A struct definition is known:
Click (here) to collapse or open
struct BOOK
{
Char TITLE[MAXTITL];
Char Author[maxautl];
float value;
};
The C99 supports the specified initialization project of the struct, whose syntax is approximate to the specified initialization of the array. Simply, the specified initialization project for the struct uses the dot operator and the member name to identify the specific element.
For example, to initialize only member value in the book struct, you can do this:
struct Book a = {. Value = 10.99};
You can use the initialization project in any order:
Click (here) to collapse or open
- struct Book gift =
- {
- . Value = 25.90,
- . Author = "Li Cong",
- . title = "Love Linux",
- };
As with arrays, a regular initialization project followed by an initialization project provides an initial value for the member following the specified member. In addition, the last assignment to a particular member is the value it actually obtains. For example:
struct Book gift =
{
. Value = 18.90,
. Author = "Li Cong",
20.0
}; This assigns the value 20.0 to the member value, because it is immediately behind the author member in the struct declaration. The new value 20.0 replaces the previous assignment of 18.90.
Attention:
1. struct specifies initialization, the use of the dot operator plus the variable name, not to indicate the type, the program will automatically match.
2. The value type on the right is as far as possible to match the left type.
3. When initializing, the variables can be separated by commas or separated by semicolons.
4. Do not forget the semicolon outside the entire structure.
The reason the kernel does this is that it does not have to be strictly in the order of definition when it is initialized, which provides great flexibility.
(go) Assignment of structs in kernel programming in Linux (struct specified initialization)