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