About assigning operations to structs in kernel programming in Linux (struct specifies initialization)

Source: Internet
Author: User

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:

    1. struct Device My_dev =
    2. {
    3. . Bus = &my_bus_type,
    4. . Parent = &my_bus,
    5. . Release = My_dev_release,
    6. };

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

    1. struct Book gift =
    2. {
    3. . Value = 25.90,
    4. . Author = "Li Cong",
    5. . title = "Love Linux",
    6. };

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)

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.