- Definition of struct type:
1 struct Student 2 {3 int 4 char name[]; 5 6 };
Note: Student is a custom data type, not a variable like Int,char is the basic data type,
- Definitions and references for struct variables:
1 struct 2{3 int ID; 4 Char name[]; 5 //Direct definition of two structural body variables
1Student Mike ={123,{'m','I','k','e'}};//defining variables and initializing them2Mike.id =20130000+ mike.id//Use3 for(inti =0; Mike.name[i]! =' /'; i++)4mike.name[i]=ToUpper (Mike.name[i]);5cout<<"ID:"<<mike.id<<"Name:"<<mike.name<<endl;//
- Storage of struct variables:
A structure variable occupies a contiguous memory space.
- Assignment of struct variables:
1 student Mike ={123, {'m','i',' k ','e'}; 2 student Lily; 3 lily = Mike; //Can be directly assigned, the corresponding variable is also assigned a value
- Structure variables as parameters of functions
The same as the variables do the function parameters, the parameters and arguments directly to the value of the transfer, copy only one copy, and the array is different from the parameter
- struct variable as return value of function
is also a copy of copy to assign value
Student mike={123,{'m','I','k','e'}};student*ps = &Mike; cout<<"ID:"<< (*PS) .id<<"Name:"<< (*PS) .name<<Endl;cout<<"ID:"<<ps->id<<"Name:"<<ps->name<<endl;
1 struct 2{3 int ID; 4 Char name[]; 5 } stu[];
- Summary: The properties of the struct data type are consistent with the characteristics of the normal data type
Structure in C language