Struct
Memory usage:Maximum data type size * Number of data types.
Definition and declaration method:
Method 1:First define the struct type, and then declare a variable of this type
Struct student {char name [20]; int number; int age; char sex; float score ;}; struct student Stu; // declare the struct student type variable
Method 2:Declare variables when defining struct types. Separate multiple declarations with commas.
Struct student {char name [20]; int number; int age; char sex; float score;} Stu; // if multiple struct student type variables are declared, the code for this row is} stu1, stu2;
Method 3:When method 2 is used, you can omit the struct type name. As follows:
Struct {char name [20]; int number; int age; char sex; float score;} Stu; // declare the struct student type variable
Method 4:Use the keyword typedef to declare the struct variable. As follows:
Typedef struct {char name [20]; int number; int age; char sex; float score;} student; Student Stu; // you do not need to write struct stuent stu at this time:
Initialization Method:
Suppose there is a definition of struct as follows:
Struct astruct
{
Int;
Int B;
};
Struct astruct test = {0 };
The initialization statement is incorrect even if astruct is a basic type member.
This initialization only sets the variable to 0 without initializing the B variable.
The cause of this error is probably the impact of array initialization. The array can be initialized in this way, and the initialization value can only be 0!
There are four types of struct initialization.
struct test{ int a; int b;};int main(){ struct test t1 = {0, 0}; struct test t2 = { .a=2, .b=3 }; struct test t3 = { a:12345, b:567890 }; struct test t4;t4.a=4;t4.b=5; printf("t1.a = %d, t1.b = %d\n", t1.a, t1.b); printf("t2.a = %d, t2.b = %d\n", t2.a, t2.b); printf("t3.a = %d, t3.b = %d\n", t3.a, t3.b); return 0;}
The 1st most common methods are GCC extensions in, which are non-standard.
Example:
# Include <iostream> using namespace STD; struct Stu {char a; int B ;}; int main () {struct Stu S1 = {'x', 1 }; struct Stu S2; s2.a = 'X'; s2. B = 2;/* struct DTU S3 = {. A = 'x ',. B = 3}; * // Dev C ++ does not support struct Stu S4 = {A: 'X', B: 4 }; cout <"S1" <s1.a <"" <s1. B <Endl; cout <"S2" <s2.a <"" <s2. B <Endl; // cout <"S3" <s3.a <"" <s3. B <Endl; cout <"S4" <s4.a <"" <s4. B <Endl; System ("pause ");}
Member reference method:
Method 1:
Struct variable name. member name
Method 2:
Pointer variable name-> member name
Method 3:
(* Pointer variable name). Member name
Struct pointer:
The general definition format is: struct name * struct pointer name
#include<iostream>using namespace std;struct Stu { char name[20]; int number; char Sex; } *p;int main(){ struct Stu s1={"HK",18,'M'}; p=&s1; cout<<p->name<<" "<<p->number<<" "<<p->Sex<<endl; struct Stu *q; q=&s1; cout<<q->name<<" "<<q->number<<" "<<q->Sex<<endl; system("pause"); }
Union
Memory usage:In the Union, each member shares a piece of memory space. The length of a contact variable is equal to the maximum length of each member.
Statement:Similar to the four statements of struct, only the keyword struct is changed to union.
Initialization:For each union object, only one of the union members can be initialized. Other Members have no value.
#include<iostream>using namespace std;union Stu { char name[20]; int number; char Sex; };int main(){ union Stu s1,*p; // s1.name="HK";//incompatible types in assignment of `const char[3]' to `char[20]' cin>>s1.name; // cin>>s1.number; // cin>>s1.Sex; p=&s1; cout<<s1.name<<" "<<s1.number<<" "<<s1.Sex<<endl; cout<<p->name<<" "<<p->number<<" "<<p->Sex<<endl; system("pause"); }
Member reference method:Same as struct.
Enumeration
The enumeration type is essentially an integer set.
#include<iostream>using namespace std;enum exam{you,liang,zhong,jige,bujige};int main(){ cout<<"size: "<<sizeof(exam)<<endl; cout<<you<<'\t'<<liang<<'\t'<<zhong<<'\t'<<jige<<'\t'<<bujige<<endl; system("pause"); }
The running result is
Size: 4
0 1 2 3 4
Memory usage:Depending on different compilers, it may be 4 in devc ++ and 1 in other compilers.
Declaration and definition:
Define types and declared variables:
Method 1:
Enum exam {You, Liang, Zhong, jige, bujige}; // defines the enumeration type.
Enum exam S1, S2; // declare an enumerated type variable
Method 2:
Enum exam {You, Liang, Zhong, jige, bujige} S1, S2;
The enumerated variables S1 and S2 are declared here, and the declared initial values are meaningless.
Define enumerated variables:
# Include <iostream> using namespace STD; Enum exam {You = 90, Liang = 80, Zhong = 70, jige = 60, bujige = 30}; int main () {Enum exam S1, S2, * P, * q; S1 = You; // defines the enumerated type variable S2 = Zhong; // defines the enumerated type variable if (S1> = S2) if (S1> S2) cout <"S1> S2" <Endl; else cout <"S1 = S2" <Endl; else cout <"S1 <S2" <Endl; P = & S1; q = & S2; cout <S1 <Endl; cout <S2 <Endl; int sum = * P + * q; cout <sum <Endl; System ("pause ");}