C language markup structure initialization syntax literacy

Source: Internet
Author: User


I have heard of the so-called C-language markup structure initialization syntax when I was studying C language, but I did not go into details when I was lazy. Recently, I learned about the Linux Device Driver and found it 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. Once again, I checked it out. I have a long knowledge. This initialization method is not a special code style, but a so-called C-language markup structure initialization syntax (designated
Initializer), and is also an ISO standard.

 
#include <stdio.h>#include <stdlib.h>struct Snooy{      int data;      void (*func1)(char*);      void (*func2)(char*);      char name[100];};void func1(char *chp){       printf("This is func1 test!\n>>%s\n",chp);}void func2(char *chp){       printf("This is func2 test!\n>>%s\n",chp);}int main(void) {     struct Snooy  val = {            .data = 1024,            .func2 = func2,            .func1 = func1,            .name = "Snooy test!"     };      val.func1("Hello test func1!");      val.func2("Hello test func2!");      return 0;}


The following is an example of my comments from the Internet:

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, programmers 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 ?!

This is a clear understanding. It turns out that this rule has these advantages. It seems that my C language skills need to be improved! You are ignorant!

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.