Three methods for C language struct Initialization

Source: Internet
Author: User

Three methods for C language struct Initialization

Go directlyExampleNow

 

# Include

 
  
Struct student_st {char c; int score; const char * name;}; static void show_student (struct student_st * stu) {printf ("c = % c, score = % d, name = % s \ n ", stu-> c, stu-> score, stu-> name);} int main (void) {// method 1: initialize struct student_st s1 = {'A', 91, "Alan"}; show_student (& s1); // method 2: Specify initialization, the order of the members is variable. This mode is usually used in Linux kernel struct student_st s2 = {. name = "YunYun ",. c = 'B ',. score = 92 ,}; show_student (& s2); // method 3: Specify initialization. The member sequence can be variable. struct student_st s3 = {c: 'C', score: 93, name: "Wood" ,}; show_student (& s3); return 0 ;}

 

 

Running result:

 

If you want to initializeStruct Array, You can use the {}, {}, {} method, as shown in figure

Struct student_st stus [2] = {{. c = 'D ',. score = 94,/* can only initialize some members */},{. c = 'D ',. score = 94 ,. name = "Xxx "},};

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.