zoukankan      html  css  js  c++  java
  • 结构体初始化测试

    最近在看Asterisk的源代码,收获不小,决定记录下来学习Asterisk过程中的点滴,分享给大家,也方便我以后查阅……

    今天让我感到意外的是Asterisk中对结构体初始化(或者说成是赋值)的使用。

    比如定义结构体如下:

    typedef struct ST {
    int a;
    int b;
    pFun fun;
    }ST;

    一般的初始化是这样的:

    ST t2;
    t2.a=4;
    t2.b=5;
    t2.fun=test2;

    而我在源码中看到的是这样的:

    ST t1 = {.a=1,.b=2,.fun=test1};

    感觉好强大。。。。。。

    这里是我仿照着写的完整代码:

     1 #include <stdio.h>
     2 
     3 typedef void (*pFun)(int a);
     4 
     5 typedef struct ST {
     6     int a;
     7     int b;
     8     pFun fun;
     9 }ST;
    10 
    11 void test1(int a)
    12 {
    13     printf("test1 : %d\r\n",a);
    14 }
    15 
    16 void test2(int a)
    17 {
    18     printf("test2 : %d\r\n",a);
    19 }
    20 
    21 int main()
    22 {
    23     ST t1 = {
    24     .a=1,
    25     .b=2,
    26     .fun=test1
    27     };
    28     
    29     printf("%d\t%d\n",t1.a,t1.b);
    30     t1.fun(3);
    31     
    32     ST t2;
    33     t2.a=4;
    34     t2.b=5;
    35     t2.fun=test2;
    36     
    37     printf("%d\t%d\n",t2.a,t2.b);
    38     t2.fun(6);
    39     
    40     return 0;
    41 }

    这里存盘为test20120608.c,进行如下操作:

    make test20120608 && ./test20120608

    测试结果:

  • E-Mail : Mike_Zhang@live.com
  • 转载请注明出处,谢谢!
查看全文
  • 相关阅读:
    Let the Balloon Rise
    Andy's First Dictionary
    myschool 1204
    vector容器
    766A Mahmoud and Longest Uncommon Subsequence
    python全栈开发-logging模块(日记专用)
    python全栈开发- day14列表推导式、生成器表达式、模块基础
    python全栈开发-Day13 内置函数
    python全栈开发-Day12 三元表达式、函数递归、匿名函数
    python全栈开发-Day11 迭代器、生成器、面向过程编程
  • 原文地址:https://www.cnblogs.com/MikeZhang/p/structuresTest1.html
  • Copyright © 2011-2022 走看看