Pointers can also point to a struct that is defined in the form of:
struct structural body name * variable name;
The following is an instance that defines the structure body pointer:
struct stu{
char *name;//name
int num;//School number
int age;//ages
char Group;//group
float score;//performance
} St U1 = {"Tom", A, ' A ', 136.5};
Structure Body pointer
struct Stu *pstu = &stu1;
You can also define a struct pointer while defining a struct body:
struct stu{
char *name;//name
int num;//School number
int age;//ages
char Group;//group
float score;//score
} stu1 = {"Tom", A, ' A ', 136.5}, *pstu = &stu1;
Note that the struct variable name and array name are different, array names are converted to arrays in an expression, and the struct variable name does not, regardless of whether it represents the entire collection itself in any expression, to obtain the address of the struct variable must be preceded by an ampersand, so assigning a value to Pstu can only be written:
struct Stu *pstu = &stu1;
and can't write:
struct Stu *pstu = stu1;
It should also be noted that the structure and struct variables are two different concepts: a struct is a data type, a template for creating variables, and the compiler does not allocate memory space for it, as int, float, and char do not occupy the same memory as the keywords themselves; The structure variables contain real data, Requires memory to store. The following is wrong, and it is not possible to take the address of a struct name or assign it to another variable:
struct Stu *pstu = &stu;
struct Stu *pstu = stu;
Get member of struct body
The structure body pointer can be used to obtain a struct member, in general form:
(*pointer). membername
Or:
Pointer->membername
In the first writing,. The precedence is higher than *, the brackets on either side of the (*pointer) are not less. If you remove the parentheses to write *pointer.membername, then it is equivalent to * (Pointer.numbername), which is completely wrong.
In the second,,-> is a new operator, used to call it an "arrow," which, with it, can be obtained directly from the structure by means of a structural body pointer, which is also the only use of-> in C language.
The above two kinds of writing are equivalent, we usually use the following wording, so more intuitive.
Use of the sample structure pointer.
#include <stdio.h>
int main () {
struct{
char *name;//name
int num;//School number
int age;
Group The group
float score//achievement
} STU1 = {"Tom", A, ' A ', 136.5}, *pstu = &stu1;
Reading values for struct members
printf ("%s's number is%d, age is%d, in%c group, this year's score is%.1f! \ n ", (*pstu). Name, (*pstu). Num, (*pstu). Age, (*pstu). Group, (*pstu). Score);
printf ("%s's number is%d, age is%d, in%c group, this year's score is%.1f! \ n ", Pstu->name, Pstu->num, Pstu->age, Pstu->group, Pstu->score);
return 0;
}
Run Result:
Tom's School number is 12, the age is 18, in Group A, this year's score is 136.5!
Tom's School number is 12, the age is 18, in Group A, this year's score is 136.5!
The use of an array pointer for the sample structure.
#include <stdio.h>
struct stu{
char *name;//name
int num;//School number
int age;//ages
char Group;//Group
float score//Results
} Stus[] = {
{Zhou ping, 5, ' C ', 145.0},
{"Zhang ping", 4,, ' A ', 130.5},
{"Liu Fang", 1, ' a ', 148. 5},
{"Cheng Ling", 2, ' F ', 139.0},
{"Wang Ming", 3,, ' B ', 144.5}
}, *ps;
int main () {
//array length
int len = sizeof (stus)/sizeof (struct stu);
printf ("name\t\tnum\tage\tgroup\tscore\t\n");
For (Ps=stus ps<stus+len; ps++) {
printf ("%s\t%d\t%d\t%c\t%.1f\n", Ps->name, Ps->num, Ps->age, ps- >group, Ps->score);
return 0;
}
Run Result:
name num Age group Score
Zhou ping 5 18 c 145.0
Zhang Ping 4 19 a 130.5
Liu fang 1 18 a 148.5
Cheng Ling 2 17 f 139.0
Wang ming 3 17 b 144.5
Structure body pointer as function parameter
A struct variable name represents the entire collection itself, the entire set passed as a function argument, all members, rather than being converted to a pointer by the compiler as an array. If there are more members in the structure, especially when the members are arrays, the time and space overhead of the transfer will be very large, which can affect the running efficiency of the program. So the best way to do that is to use the struct pointer, when the argument is passed to the formal parameter by just one address, very fast.
The example calculates the total score of the class, the average score, and the number of people below 140 points.
#include <stdio.h>
struct stu{
char *name;//name
int num;//School number
int age;//ages
char Group;//Group
float score//Results
} Stus[] = {
"Li ping", 5,, ' C ', 145.0},
{"Zhang ping", 4,, ' A ', 130.5},
{"He fang", 1, ' A ', 148.5},
{"Cheng Ling", 2, ' F ', 139.0},
{"Wang Ming", 3, ' B ', 144.5}
};
void average (struct stu *ps, int len);
int main () {
int len = sizeof (stus)/sizeof (struct stu);
Average (Stus, Len);
return 0;
}
void average (struct stu *ps, int len) {
int i, num_140 = 0;
float average, sum = 0;
For (i=0 i<len; i++) {
sum + = (PS + i)-> score;
if (PS + i)->score < 140) num_140++;
}
printf ("sum=%.2f\naverage=%.2f\nnum_140=%d\n", Sum, SUM/5, num_140);
}
Run Result:
sum=707.50
average=141.50
num_140=2
The above is the structure and pointers to the data collation, follow-up continue to supplement the relevant information, thank you for your support for this site!