Struct initialization Test

Source: Internet
Author: User

I recently read the source code of Asterisk. I decided to record some of my work in learning Asterisk and share it with you. It is also convenient for me to refer to it later ......
What surprised me today is the use of the struct initialization (or assignment) in Asterisk.
For example, the struct is defined as follows:
Typedef struct ST {
Int;
Int B;
PFun fun;
} ST;
The general Initialization is as follows:
ST t2;
T2.a = 4;
T2. B = 5;
T2.fun = test2;
What I see in the source code is as follows:
ST t1 = {. a = 1,. B = 2,. fun = test1 };
Feeling strong ......
Here is the complete code I wrote following the example:
 
1 # include <stdio. h>
2
3 typedef void (* pFun) (int );
4
5 typedef struct ST {
6 int;
7 int B;
8 pFun fun;
9} ST;
10
11 void test1 (int)
12 {
13 printf ("test1: % d \ r \ n", );
14}
15
16 void test2 (int)
17 {
18 printf ("test2: % d \ r \ n", );
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}
 
Save the disk test20120608.c and perform the following operations:
Make test20120608 &./test20120608
Test results:

 





From MikeZhang's blog

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.