09-- structure

Structure

C language allows users to create a data structure consisting of a combination of type composed of different types of data themselves, it is referred to the structure

The general form of a statement of the type of structure:

structure struct {name} tabular member;

Student struct 
{ 
    int NUM; 
    char name [20 is]; 
    char Sex; 
    int Age; 
}; 
braces is a child of the structure contains, referred to the members of the structure, the above-described num, name, sex, age are It is a member. 
Each member should be the type of statement that 
    the type of member name;

Members of the table column also referred to as "domain table", each member of a domain is a structural body

 

Definition of the structure type variable

1, the structure type of the first statement, and then define the types of variables

Student struct 
{ 
    int NUM; 
    char name [20 is]; 
    char Sex; 
    int Age; 
}; 
 struct student1, STUDENT2; 
structure type variable name structure name

2, in a statement at the same time define a variable of type

Student struct 
{ 
    int NUM; 
    char name [20 is]; 
    char Sex; 
    int Age; 
} student1, STUDENT2; 
the general form: 
struct structure name 
{ 
    members listed 
} variable watches column;

3, do not specify the name of the type definition of the structure and the direct type variable

General form: 
struct 
{ 
    members listed 
} variable watches column;

Note: Members of the structure type name may be the same variable name in the program, but they do not represent the same object

 

Structure variables are initialized and references

1, when the definition of the structure variables can be initialized to its members

2, the structure can be cited a member variable value, reference to:

Structure variable Members

3, if the type of structural member which itself has a structure belonging to another type, the use of several members of the operator to find a lowest level of a member, the member only to the lowest level of access and assignment or operations.

Reference member method:

student1.num (structure variable student members in NUM) 
student2.birthday.month 
(student2 member birthday structure variable of the members month)

4, the structure members may be variable calculation

student2.score = student1.score; (assignment operator) 
SUM = student2.score + student1.score; (adder) 
student2.age ++ (from plus operation)

5, the same kind of structure variable can be assigned to each other

student2  = student1

6, the address of the variable members can refer to the structure, you can refer to the address of the structure variable

scanf ( "% d", & student2.score); ( student2.score input value) of 
the printf ( "% D", & STUDENT2); (output variable structure of the start address STUDENT2)

Address structure variable as a function of the main parameters of the address structure variable transmission

 

Use array of structures

A structure variable can be stored in a set of data associated, if there are 10 students in need of data operations, should obviously be used in an array, this is the array of structures

Defines an array of structures general form:

. 1, 
  struct structure name 
    {members} tabular array name [array length] 
2, 
     declare a structure, and then define an array of structures of this type: 
      the type of structure array name [array size] 
      as: struct Person leader [3]; // leader structure is an array name

Initialization of an array structure is defined later in the array plus:

} = {Initial value table column;

例如:struct Person leader[3] = {"li",0,"zhang",0,"wang",0};

 

Structure pointer

Structure pointer variable is a pointer to a structure, the start address of a structure variable is a pointer to this structure variables.

If the start address of a structure variable is stored in a pointer variable, then the pointer variable points to the structure variables.

Pointer to a variable structure

Pointing structure pointing to the object pointer variable structure variables can also be directed in the array element structure.

Base type pointer variables must be the same type of structure variables.

For example: struct Student * pt // pt may be directed to a variable of type struct Student or array elements
struct Student 
    { 
        Long NUM; 
        char name [50]; 
        char Sex; 
        a float Score; 
    }; 
    struct Student stu_1; // define a variable of type struct Student stu_1 
    struct Student * P; // define a pointer variable p struct Student types of data 
    & stu_1 = P; 
    the printf ( "NO:% LD \ nname:% S \ nsex:% C \ NSCore:. 5.lf% \ n-", 
        stu_1.num, stu_1.name, stu_1.sex, stu_1.score); 
                is equivalent to 
    the printf ( "NO:.% LD \ nname:% S \ nsex:% C \ NSCore: 5.lf% \ n-", 
        (* .num P), (P *) .name, (P *) .sex, (* p) .score); 
C language allows (* p) .name with p-> place name; 
p-> name represents a structure variable name members pointed in the p 
"->", said to point to the operator 
if a structure variable p at the stu, the following three kinds of usage is equivalent to: 
1, stu members (eg: stu.name).
2, (* p) member name. (Such as: (P *) .name) 
. 3, p-> Member Name (eg: p-> name)

Pointer to an array of structures

struct Student stu[3] = {
{10101,"mao",'M',78},
{10103,"li",'F',98},
{10106,"zhang",'M',86},
};

struct Student* p; //定义指向struct Student 结构体变量的指针变量
	printf("学生的全部信息\n");
	for (p = stu; p < stu + 3; p++)
	{
		printf("%5d %-20s %2c %4d\n",p->num,p->name,p->sex,p->age);
	}

And a pointer to a structure with the structure variables as a function of the variable parameter

The structure of a variable value to another function, there are three methods:

  1. A member structure variable as a parameter. For example: stu [1] .num as function arguments

  2. A structure variable as argument.

  3. Pointing towards the structure variable (or array element) as an argument a pointer to the address of the structure variables (or array element) is passed parameter.

Published 16 original articles · won praise 2 · Views 129

Guess you like

Origin blog.csdn.net/weixin_42248871/article/details/105258086