Definition and use of structure

. 1 #include <stdio.h>
 2  
. 3  // structure keyword struct
 . 4  // define a game player NPCs 
. 5  struct Gamer
 . 6  {
 . 7      char cName [ 24 ];     // name of the player 
. 8      int nHealth; // health 
. 9      int NMAGIC;     // magic 
10      int nSkil;     // skills 
. 11      a float fExperience;     // experience 
12 is      int nBlood;     // blood values 
13 is      intnGrade;     // Level 
14      a float fMovSpeed;     // moving speed 
15  };
 16  int main ( void )
 . 17  {
 18 is      // of a structure initialized 
. 19      struct Gamer G1 = { 0 };     // use a structure name defined variable, and the member variable is initialized to all zero
 20 is  
21 is      // of the two initialized structure 
22 is      struct Gamer G2 = { " I chicken dish " };     // use structure define a variable name, and initializes each member the value of the variable
 23 is  
24      // for a structure three initialization method, each of the member variable initialization gave 
25      structG3 = Gamer
 26 is      {
 27          , " I was a novice " ,     // the name 
28          100 ,     // life value 
29          90 ,     @ magic 
30          99 ,     // skill 
31 is          88 ,     // experience 
32          77.3 ,     // blood values 
33 is          100 ,     // level 
34          66.6     @ moving speed 
35      };
 36      struct Gamer G4;
 37 [      //g4.cName = "chicken dish No. 3";     // this writing error, error, char type array does not support "=" 
38 is      strcpy (g4.cName, " food chicken No. 3 " );     // herein strcpy function, 
39      the printf ( " % S \ n- " , g4.cName);
 40      the printf ( " % D \ n- " , g4.fExperience);     // body member variables to the structures not initialized, will produce a garbage value -2147483648 
41 is  
42 is      G4 = .fExperience 224.3f ;     // initialize variables member 
43 is      the printf ( " %. 2F \ n- " , g4.fExperience);    
 44 is  
45      the printf (" % S \ n- " , g3.cName);     // Print I chicken dish 
46 is      return  0 ;
 47 }

 

Guess you like

Origin www.cnblogs.com/axuanup/p/12643142.html