structure, structure array, structure pointer

・・・・Objective・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・
Use · Definition and use of structure pointer





1. Definition
syntax of structure type: The general form of defining structure type is as follows

  struct structure type name
 {
  data type 1 member 1 ;
   data type 2 member 2 ;


   data type n member n ;
  } ;
Semantics: Define a structure type with n members. Note that the compiler does not allocate storage space for the type
. Example: Define the structure type DateType to represent information such as year, month, and day.
struct DateType
{
int year ;
int month ;
int day ;
};
Structure types can be defined nested, for example:
struct StudentType
{
char name[20];
char gender[3];
struct DateType birthday ; //struct DateType is defined type
};

2. Definition
syntax of structure variables: The general form of defining structure variables is as follows:
(1) First define the structure type, and then define the structure variable
struct structure type name variable name list;
(2) While defining the structure type Define structure variable
struct structure type name
{
 member list
} variable name list;
semantics: define the variables in the variable name list as structure type, and the compiler allocates storage space for structure variables.

struct DateType //First define the structure type
{
int year, month, day;
};
struct DateType birthday; //Then define the structure variable birthday

struct DateType
{
int year, month, day;
} birthday; //Define the structure variable birthday while defining the structure type
3. Reference to the structure variable

Syntax: The general form of referencing a structure variable is as follows:
structure variable name. member name


·Initialization of structure variables
(1) First define the structure type, then define the structure variable and assign the initial value
struct structure type name variable name = {initial value of member 1, ..., initial value of member n};

(2) When defining the structure type, define the structure variable and assign the initial value
struct structure type name
{
   declaration member type
} variable name = {initial value of member 1, ..., initial value of member n};
semantics: Define structure variables and initialize them.
Example: struct DateType
{
int year;
int month;
int day;
};
struct DateType birthday = {1968, 3, 26};

struct DateType
{
int year;
int month;
int day;
} birthday = {1968, 3, 26};

· Operation of structure variables

1. Input/output operation
In C language, structure variables cannot be used as arguments of scanf function and printf function. Each member of the variable performs input/output operations.
struct DateType birthday;
scanf ("%d%d%d", &birthday.year, &birthday.month, &birthday.day );
printf("%d-%d-%d", birthday.year, birthday.month, birthday .day );
2. Assignment operation: assign values
​​to its members one by one in the program, but not as a whole.
struct DateType birthday;
birthday = {1997, 1, 4};
this is wrong!
The correct one should be:
struct DateType birthday1 = {1997, 1, 4}, birthday2;
birthday2 = birthday1;
if the types of the two structure variables are the same, the overall assignment can be performed in the program.
3. Other operations:
The C language does not define the operations applied to the structure type, and the operation of the structure variable is realized by the operation of its members.
Essentially, a member of a structure variable is a simple variable, so its members can be used in the same way as simple variables of the same type.
struct DateType birthday1 = {1968, 3, 26}, birthday2;
int year;
birthday2.year = 1963;
year = birthday1.year - birthday2.year; //The number of years between the two structure variables

·Structure array
1·Definition of structure array variable
struct StudentType
{
char no[10] ;
char name[10] ;
double foreign ;
double spec1 ;
double spec2 ;
} ;
struct StudentType stud[10] ;
(1) Structure Array reference
syntax: The general form of referencing a structure array is as follows:
Structure array name [subscript]. Member name
semantics: Refer to a specific member of a structure array.
(2) Initialization of structure array variables
struct StudentType
{
char no[10];
char name[10];
double foreign;
double spec1;
double spec2;
} stud[10] ={{001, Lu Yu, 87, 67, 88}, {002, Li Ming, 68, 85, 78}};

struct StudentType stud[10] ={{001, Lu Yu, 87, 67, 88}, {002, Li Ming, 68, 85, 78}};

2. Operation
of structure array elements Since each element of the structure array is data of a structure type, the use method of the structure array element is the same as that of the structure variable. The method of use is the same as that of simple variables of the same type.
struct StudentType stu[5] ={{0001, Lu Yu, 87, 67, 88}};
stu[1] = stu[0]; /*The elements of the structure array can be assigned directly*/
stu[2]. foreign = 98;
strcpy(stu[2].name, "Wang Qi");
printf("The difference in foreign language grades is %6.2f\n", stu[2].foreign - stu[1].foreign);


·Definition and use of structure pointer
Among them, the structure type is a defined or being defined structure type; the pointer variable name is a legal identifier.
Semantics: Define a pointer to a struct type.
After defining a structure pointer, you need to bind the pointer to the address of a structure variable. For example, the following statement declares and initializes a pointer p to the structure.
struct DateType
{
int year, month, day;
};
struct DateType birthday = {1968, 3, 26};
struct DateType *p = &birthday;
access structure members by pointer

A.(*pointer).Member
Among them, "*pointer" must be enclosed in parentheses, because the priority of the member operator "." is higher than that of the indirect reference operator "*".
B. Pointer->member
semantics: refer to a specific member of a structure variable through a structure pointer.
Example: struct DateType
{
int year, month, day;
};
struct DateType birthday = {1968, 3, 26};
struct DateType *p = &birthday;
printf("%4d-%2d-%2d\n", p- >year, p->month, p->day);
Note: The structure pointer is used as a function parameter
to pass the structure variable to the function. There are three ways:
the formal parameter is a structure member, and the actual parameter is the corresponding structure member Value, parameter passing is to pass the value of the structure member to the formal parameter.
The formal parameter is the structure variable, the actual parameter is the value of the structure variable, and the parameter passing is to pass the value of the structure variable to the formal parameter.
The formal parameter is the pointer to the structure type, the actual parameter is the address of the structure variable or the pointer to the structure variable, and the parameter transfer is to pass the first address of the structure variable to the formal parameter.
The first two methods belong to the value transfer method. When the structure is large, the space and time overhead is large, and it is generally less used.

 

 

/*University all the way C*/

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325869588&siteId=291194637