Structure:
Type structure is a structure, which is composed of several "members" of the composition, wherein each of the members can be a base data type may be a type of configuration.
Structure is a new type, we need to be declared.
Shaped like:
Any type of previously learned "fruit" of this type does not use expressed the need to define a new type, called a structure.
A declaration structure is as follows:
struct structure name
{
Member list
};
Structure name indicates the name of the type of structure, members constituting the parentheses variable structure.
Structure variable definition:
Declares a structure represents a new type name, and to define the variable
1, the statement structure, and then define variables:
2, when the structure type declaration, the definition of variables:
Variable is defined in the form:
struct structure name
{
Member list;
} Variable name list;
Note: Only one variable can not be defined, you can define multiple variables.
3, the direct type variable definition of the structure:
In the form of:
struct
{
Member list
} Variable name list;
Structure variable references:
After the definition of the structure type variable, this variable can refer to, to the structure variable assignment, access or operation.
Members form the reference variable structure is as follows:
Structure variable name, member name
Problem: the definition of a teacher representation of the structure, members of the structure have a name, age, seniority; use the structure to define a teacher, and assignments, and then output. code show as below:
Structure initialization:
Structure type specifies an initial value when the definition of the structure variables.
Array of structures:
Defined array of structures:
In the form of:
struct structure name
{
Member list;
} Array name:
Initialization array of structures:
Initializing an array form body structure:
struct structure name
{
Member list;
} = {Initial value array name list};
Problem: A double 11 sites do promotional activities, by using a structure array of programming, the pre-sales information output 5, the sales of the product sales and 5 below. code show as below:
Structure pointer:
Pointer to a structure variables:
Pointer to the structure variable of the address pointer may be used to access members of the structure.
Definition of the structure in the form of pointers:
* Pointer to structure type name;
Use a pointer pointing to a structure variable access to members of the following two methods:
The first method is to use the dot operator reference structure members.
(* PStruct). Member Name
Computing structure can be referenced using the dot operator. * PStruct represents a structure variable points.
Note: * pStruct to use parentheses.
The second method is to use a reference point operator structure members.
pStruct -> Member Name;
Pointer to an array of structures:
结构体指针变量直接指向结构体数组中的元素,指针变量的值就是该结构体数组元素的地址。
形如:
struct student * pStruct;
pStruct=student;
结构体作为函数参数:
将结构体变量的值作为一个函数的参数。使用结构体作为函数的参数有3中形式。
1、使用结构体变量作为函数参数:
使用结构体变量作为函数的实际参数时,将结构体变量中的内容按顺序传递给形式参数。
形如:
void Display(struct student stu);
2、使用指向结构体变量的指针作为函数参数:
使用结构体变量的指针作为函数参数进行传递内容。
形如:
void Display(struct student *stu);
3、使用结构体变量的成员作为函数参数:
使用此方式为函数传递参数是传值方式传递。
形如:Display(student.fscore[0]);
共用体:
共用体的概念:
共用体也称为联合体,它使几种不同类型的变量存放到同一段内存单元中。在同一时刻只能有一个值。
共用体的大小等于最大内存的大小。
形如:
其中三个圆共同交汇的区域就可以理解为是这三个圆的共用体。
定义共用体的类型变量为:
union 共用体名
{
成员列表
}变量列表;
共用体变量的引用:
共用体引用的形式为:
共用体变量,成员名;
习题:公司员工下班回家,可以坐出租车,可以坐公交车,可以坐地铁,设计一个交通工具的共用体,让员工进行选择。代码如下:
共用体变量的初始化:
在定义共用体变量,初始化的值放在一对大括号中。
枚举类型:
利用关键字enum可以声明枚举类型。使用该类型可以定义枚举类型变量,一个枚举变量包括一组相关的标识符,其中每个标识符都对应一个整数值。
形如:将果盘中的水果名称定义一个枚举类型变量,其中每个标识符都对应一个整数值。
enum Fruits(Watermelon,Mango,Grape,Orange,Apple);
Fruits就是定义的枚举类型变量,在括号中的第一个标识符对应着数值0,第二个对应于1,依此类推。
习题:定义枚举类型,代表一年中的四个季节,给”季节"枚举类型分别赋值,并用整形格式输出四个季节的值。代码如下: