Assignment of an alternative struct in C Language

Source: Internet
Author: User

 

Looking at the Linux kernel source code over the past few days, it is very strange to see the assignment of the struct variable init_pid_ns.

 

Struct pid_namespace init_pid_ns = {

. Kref = {

. Refcount = ATOMIC_INIT (2 ),

},

. Pidmap = {

[0... PIDMAP_ENTRIES-1] = {ATOMIC_INIT (BITS_PER_PAGE), NULL}

},

. Last_pid = 0,

. Level = 0,

. Child_reaper = & init_task,

};

Note the value assignment form of the struct array. pidmap in the above Code.

 

[0... PIDMAP_ENTRIES-1]

The index is assigned directly, and the index range is written directly. A little python flavor.

 

I just remember that the variable parameters of C-language functions are in the... method. I never thought about this operation method for struct.

 

Interested. I wrote a short piece of similar code myself.

 

# Include <stdio. h>

 

Struct pid {

Int val;

Void * fn;

};

 

Struct str {

Int val;

Struct pid id [10];

};

 

Struct str init_str = {

. Val = 1,

. Id = {

[0... 9] = {2, NULL}

},

};

 

Int main (void ){

Int I;

 

For (I = 0; I <10; I ++)

Printf ("init_str.id [% d]. val = % d \ n", I, init_str.id [I]. val );

 

Return 0;

}

 

Compile and test. The result is true.

 

If you want to operate the struct array, try this method.

 

From wfing

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.