本实例通过结构体指针变量实现在窗体上显示学生信息,运行程序后,将学生信息输出在窗体上,效果如图所示:
在本实例中,我们先声明一个结构体变量,并定义一个指针变量指向struct student类型的数据,代码如下:
#include<stdio.h>
#include<stdlib.h>
struct student{
int num; //学生学号
char name[20]; //学生姓名
char sex; //学生性别
int age; //学生年龄
float score; //学生成绩
};
void main()
{
struct student student1={1001,"liming",'M',20,92.5}; //定义结构体变量
struct student *p; //定义指针变量指向结构体类型
p=&student1; //使指针指向结构体变量
printf("Number:%d\n",p->num); //输出学生学号
printf("Name:%s\n",p->name); //输出学生姓名
printf("Sex:%c\n",p->sex); //输出学生性别
printf("Age:%d\n",p->age); //输出学生年龄
printf("Score:%2.1f\n",p->score); //输出学生成绩
system("pause");
}
一个结构体变量的指针就是该变量所占据的内存段的起始地址,用一个指针变量指向一个结构体变量,此时该指针变量的值就是结构体变量的起始地址