如何取出结构体中的每一个成员变量:
#include <stdio.h>
struct Student
{
int age;
float score;
char sex;
};
int main(void)
{
struct Student st = {80, 66.6F, 'F'};
struct Student * pst = &st;
pst->age = 88;
st.score = 66.7f;
printf("%d %f\n", st.age, pst->score);
return 0;
}
取出结构体变量中的成员值有两种方式:
第一种:
结构体变量名.成员名
第二种:
指针变量名->成员名
说明:此处的指针变量是指结果体类型指针变量,如:
struct Student * pst,指针变量名是pst,
pst中只能存放struct Student类型变量的地址
所以有struct Student * pst = &st;
另外需要说明的是:
pst->age
表示pst变量所指向的结构体的age成员变量
pst->age等价于(*pst).age等价于st.age