linux kernel 结构体赋值方法{转载}

原文地址:

http://www.chineselinuxuniversity.net/articles/48226.shtml

这几天看Linux的内核源码,突然看到init_pid_ns这个结构体变量的赋值特别奇怪。 view plainprint? 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, }; 请注意上面代码的.pidmap这个结构体数组的赋值形式。 view plainprint? [ 0 ... PIDMAP_ENTRIES-1] 直接拿索引来赋值,而且直接写出了索引的范围。有点python的味道。

只是记得C语言的函数的可变参数是用的...的方式。没想过结构体也有这种操作方式。

比较感兴趣,自己写了一小段类似的代码。 

 

 1 #include <stdio.h>
 2 
 3 struct pid{   
 4 
 5   int val;   
 6 
 7   void *fn; 
 8 
 9 };
10 
11 struct str{  
12 
13    int val;    
14 
15  struct pid id[10]; 
16 
17 };
18 
19 struct str init_str = { 
20 
21     .val = 1,  
22 
23    .id = {         [ 0 ... 9] = {2, NULL}     }, 
24 
25 };
26 
27 int main(void){     int i;
28 
29     for(i = 0; i < 10; i++)         printf("init_str.id[%d].val =  %d\n", i, init_str.id[i].val);
30 
31     return 0; 
32 
33 }

 

 

编译后测试。结果的确是这样。

如果要对结构体数组进行操作,这种方式可以试试。

 

 

下面的代码是自己写的测试代码:

 

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <stdbool.h>
 4 //use the linux kernel struct init method
 5 // Write by sndnvaps<sndnvaps@gmail.com>
 6 //struct stu {
 7 // int age;
 8 // float tall;
 9 // char Address[256];
10 // char CellPhone_num[30];
11 // char sex[5];
12 // };
13 // 
14 // *****************
15 // init the struct 
16 // ****************
17 // struct stu stu_one = {
18 // .age = 18,
19 // .tall = 175.6,
20 // .Address = "GuangZhou,GuangDong,China",
21 // .CellPhone_num = "189xxxxxxxx",
22 // .sex = "male",
23 // };
24 struct stu_info{
25     int age;
26     float tall;
27     char Address[256];
28     char CellPhone_num[30];
29     char sex[10];
30 };
31 
32 struct stu_info stu = {
33         .age = 17,
34         .tall = 175.6,
35         .Address = "GuangZhou, GuangDong, China",
36         .CellPhone_num = "189xxxxxxxx",
37         .sex = "male",
38     };
39 
40 
41 bool check_age(struct stu_info *stu) {
42     if (stu->age >= 18) {
43         return true;
44     }
45     return false;
46 }
47 
48 int main(int argc, char *argv[]) {
49     bool is_eightheen = false;
50     is_eightheen = check_age(&stu);
51     printf("infomation of stu one ....\n");
52     printf("Age = %d\n", stu.age);
53     printf("Tall = %f\n", stu.tall);
54     printf("Address = %s\n", stu.Address);
55     if (is_eightheen) {
56 
57     printf("Cellphone number = %s\n", stu.CellPhone_num);
58     } else {
59         printf("this male under 18, not show the cellphone number..\n");
60     }
61 
62     printf("Sex = %s\n", stu.sex);
63     return 0;
64 }

 

  

转载于:https://www.cnblogs.com/sn-dnv-aps/p/3150797.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值