Pointer to struct
Outputs student information using pointer pointing to struct Array
Note: If the Pointer Points to the struct array, the starting address of the struct array is obtained. This address can be used to access all member variables in the struct array. The arithmetic operation of pointer to struct is similar to that of pointer to array.
# Include
# Define N 10 // struct type and variable definition, initialize struct student {char * number; char * name; char sex; int age; float score ;} stu [3] = {06001, Wang Chong, 'M', 22, 78.5}, {06002, Li Hua, 'F', 21, 87.0}, {06003, zhang Yang, 'M', 22, 90.0 }}; void main () {struct student * p; printf (basic student information table :); printf (Name: Gender and Age score ); for (p = stu; p
Number, p-> name, p-> sex, p-> age, p-> score );}
The running result is as follows;
In the above program, a pointer Variable p pointing to the struct is defined first. In the loop body, the Pointer Points to the struct array p = stu, that is, the Pointer Points to the starting address of the struct variable. Access members through p-> number, p-> name, and so on. P + 1 indicates the starting address of the second element stu [1] In the array. P + 2 indicates the starting address of the third element in the array, if:
During struct definition, the keyword typedef is usually used to create an alias for the defined data type. For example:
Typedef struct student StuInfo;
The alias of student Of the struct type is defined as StuInfo, so that StuInfo becomes a struct type. StuInfo can be used to define struct variables and pointer variables, for example:
StuInfo stu [3], * p;
You can also use typedef to create an alias for the basic data type, for example:
Typedef int DataType; // The DataType is equivalent to the int type.
You can use the keyword typedef to make the program more portable.