zoukankan      html  css  js  c++  java
  • 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
    ", 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 ....
    ");
    52     printf("Age = %d
    ", stu.age);
    53     printf("Tall = %f
    ", stu.tall);
    54     printf("Address = %s
    ", stu.Address);
    55     if (is_eightheen) {
    56 
    57     printf("Cellphone number = %s
    ", stu.CellPhone_num);
    58     } else {
    59         printf("this male under 18, not show the cellphone number..
    ");
    60     }
    61 
    62     printf("Sex = %s
    ", stu.sex);
    63     return 0;
    64 }

      

  • 相关阅读:
    实验二 用机器指令和汇编指令编程
    实验1查看CPU和内存,用机器指令和汇编语言指令编程
    汇编第9~15章——汇编基础知识梳理与总结
    汇编第5~8章——基础知识梳理与总结
    实验9 根据材料编程
    实验5 编写、调试具有多个段的程序
    实验4 [BX]和loop指令
    实验3 编程、编译、连接、跟踪
    实验2 用机器指令和汇编指令编程
    实验1 查看CPU和内存,用机器指令和汇编指令编程
  • 原文地址:https://www.cnblogs.com/sn-dnv-aps/p/3150797.html
Copyright © 2011-2022 走看看