On the struct initialization of the structure of the number ".", the array [] initialize the extra comma ","

Source: Internet
Author: User

The structure body struct Initialize the extra point number "."

When reading the Gnu/linux kernel code, we encounter a special form of structural initialization, which is not covered in the book. This is referred to as the specified initialization (designated initializer).


Let's take a look at an example: there is such a struct initialization project in LINUX-2.6.X/DRIVERS/USB/STORAGE/USB.C:

static struct Usb_driver Usb_storage_driver = {
. Owner = This_module,
. Name = "Usb-storage",
. Probe = Storage_probe,
. Disconnect = Storage_disconnect,
. id_table = Storage_usb_ids,

};
At first glance, this is far from the initialization of the structures we have learned before. In fact, that's what we said earlier. Specifies an application to initialize in a Linux device driver, which originates from the ISO C99 standard.

I have abstracted the contents of the relevant chapters in the C Primer plus fifth edition so that it is possible to understand that the 2.6 kernel's advantage in this way is that this initialization does not have to be strictly in the order of the definitions.

A structure is known, defined as
struct Book {
Char TITLE[MAXTITL];
Char Author[maxautl];
float value;

};

1 The C99 supports the specified initialization project of the structure, which is approximately the syntax of the specified initialization project for the array. Only the specified initialization project uses the dot operator and the member name (instead of square brackets and index values) to identify the specific element.

For example, to initialize only the member value of the book structure, you can do this:

struct Book surprise = {. Value = 10.99};

2 You can use the specified initialization project in any order:

struct Book gift = {. Value = 25.99,. Author = "James Broadfool",. title = "Rue for the Toad"};

Like arrays, a regular initialization project following a specified initialization project provides an initial value for the member following the specified member.

3 The last assignment to a particular member is the value it actually obtains.

For example, consider the following statement:

struct Book gift = {. Value = 18.90,. Author = "Philionna pestle", 0.25};

This assigns the value 0.25 to member value, because it is immediately following the author member in the structure declaration. The new value of 0.25 replaces the previous assignment of 18.90. Further information about designated initializer can be referred to the C99 Standard 6.7.8 Section ininialization.


4) Specific initialization
Standard C89 elements that require initialization statements appear in a fixed order, as in the order of elements in an array or structure that is initialized. In the ISO C99, you can give these elements in any order, indicating the names of the subscripts or structs of their corresponding arrays, and GNU C also takes this as an extension of the C89 mode (this extension is not implemented in GNU C + +). To specify an array subscript, write "[index] = xx" in front of the element value.

For example: int a[6] = {[4] = 29, [2] = 15}; equivalent to: int a[6] = {0, 0, 15, 0, 29, 0};


The subscript value must be a constant expression, even if the initialized array is automatic. An alternative to this syntax is to write "in front of the element value." [index] ", no" = ", but is no longer used since GCC 2.5, but GCC is still accepted. To initialize a series of elements to the same value, write "[first ... last] = value". This is a GNU extension.

For example: int widths[] = {[0 ... 9] = 1, [10 ... 99] = 2, [100] = 3};
If the value has a side effect, this side effect occurs only once, not every time within the range.

Note that the length of the array is the maximum value specified plus one.

Reproduced above from http://zhouyang340.blog.163.com/blog/static/3024095920123495051607/

array initializes the extra comma ","

The following initialization of a struct defined in U-boot 2008.10 drivers/net

static struct {
	const char *name;
	U8 version;		/* Depend on RTL8169 docs * *
	u32 rxconfigmask;	/* Should clear the BITS supported by this chip/
} rtl_chip_info[] = {
	{"RTL-8169", 0x00, 0xff7e1880,},
	{"RT L-8169 ", 0x04, 0xff7e1880,},
	{" RTL-8169 ", 0x00, 0xff7e1880,},
	{" rtl-8169s/8110s ",	0x02, 0xff7e1880,},
	{"Rtl-8169s/8110s",	0x04, 0xff7e1880,},
	{"RTL-8169SB/8110SB",	0x10, 0xff7e1880,},
	{" RTL-8169SC/8110SC ",	0x18, 0xff7e1880,},
	{" RTL-8168B/8111SB ",	0x30, 0xff7e1880,},
	{" rtl-8168b /8111SB ",	0x38, 0xff7e1880,},
	{" rtl-8101e ", 0x34, 0xff7e1880,		},
	{" rtl-8100e ",		0x32, 0xff7e1880,},
};


Directly after the definition of the structure, the key to the final surface of a ",", arguably only 3 members, why.

Do not worry, please see the following definition is also in U-boot 2008 driver/usb/usbdcore.c

Char *usbd_device_states[] = {"
	state_init",
	"state_created",
	"state_attached",
	"State_ Powered ",
	" State_default ",
	" state_addressed ",
	" state_configured ",
	" State_unknown ",
};


See the array at the end there is also a "," The Forum also explored the issue

http://bbs.csdn.net/topics/330052999

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.