Markup structure initialization syntax (C language)

Source: Internet
Author: User

I used to watch LinuxCodeIt is strange to write the initialization method of its struct. All the initialization code has written the variable name, and there is a strange point in front of the variable name. I recently learned about the Linux Device Driver and encountered it again. I checked it and found that my knowledge was missing a lot. This initialization method is not a special code style, it is the so-called C language markup structure initialization syntax (designated initializer), and it is still an ISO standard.

 

 

[CPP] View plaincopy

  1. # Include <stdio. h>
  2. # Include <stdlib. h>
  3. StructOperators
  4. {
  5. Void(* Read1 )(Char*);
  6. Void(* Read2 )(Char*);
  7. Void(* Read3 )(Char*);
  8. IntN;
  9. };
  10. VoidRead1 (Char* Data)
  11. {
  12. Printf ("Read1: % S/N", Data );
  13. }
  14. VoidRead2 (Char* Data)
  15. {
  16. Printf ("Read2: % S/N", Data );
  17. }
  18. VoidRead3 (Char* Data)
  19. {
  20. Printf ("Read3: % S/N", Data );
  21. }
  22. IntMain ()
  23. {// Traditional Initialization Method
  24. // Struct operators my_op = {read1, read2, read3, 100}; // so-called markup structure initialization syntax
  25. StructOperators my_op = {. read2 = read2,
  26. . Read1 = read1,
  27. . Read3 = read3,
  28. . N = 100 };
  29. My_op.read1 ("Wangyang");
  30. My_op.read2 ("Wangyang");
  31. My_op.read3 ("Wangyang");
  32. Return0;
  33. }

 

 

The key lies in the initialization Statement of my_op struct in the main () function, which is initialized by adding the vertex variable name. People who have used python will immediately feel how similar this is to passing parameters with keywords.

 

So where is the benefit? I think there are three advantages:

First, mark the order in which parameters are passed without consideration. As shown in the preceding example, I first initialized read2 and then initialized read1,ProgramMembers do not need to remember the order of parameters;

Second, we can choose to pass parameters. In the traditional C language, if you only want to initialize the third variable, You have to initialize the first and second parameters, sometimes a variable does not have a suitable default value, but with the tag initialization method, you can initialize parameters that you are sure;

Third, better scalability. If you want to add a field to the struct, you 'd better put the newly added field at the end of the struct to consider the amount of code modification, otherwise, you will face a lot of uninteresting changes. You may think it's okay to put them somewhere, but we all get used to it. The names are gender, the gender is age, and then the hobbies, the last part is the story description. If the age is at the end, isn't it awkward ?!

 

Some people have mentioned that this syntax is also conducive to improving performance. I will not talk about this here.

 

In fact, this initialization syntax is not a new technology, a new definition, it is a standard usage of ISO c99, that is, 99 years, and Linus will not catch up with anything fashionable, it is said that C primer plus mentioned this in the fifth edition. However, I have not read this book. Unfortunately, I am directly engaged in object-oriented development.

 

GCC has the extension markup structure initialization syntax, which is written as follows:

Struct operators my_op = {read2: read2, read1: read1, read3: read3 ,};

 

 

From: http://www.hustyx.com/cplusplus/agtodXN0eWl4aWFuZ3IPCxIHQXJjaGl2ZRihigcM

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.