结构体定义:
struct student //定义了一个结构体
{
long stuid;
char name[10];
int age;
int score;
}stu1; //定义了一个结构体变量
定义结构体变量:
struct student stu2;
定义结构体指针:
struct student *stu3;
指针初始化:
(一)
stu3=&stu1;
(二)或者:
struct student *stu3=&stu1; //定义的同时初始化
struct student *stu3;
*stu3=stu1;
定义结构体数组:
struct student stu4[10]; //定义了一个结构体数组
参考代码:
#include <stdio.h>
int main()
{
typedef struct information //定义了一个结构体
{
long stuid;
char name[10];
int age;
int score;
}info; //别名
info stu[40]; //定义了一个结构体数组
info *pt; //定义了结构体指针
pt = stu; //指针初始化方法一
info *pt1 = &stu[0]; //初始化方法二
info *pt2 = stu; //方法三
}
访问结构体变量成员:
方法一:成员选择运算符
stu1.age=18;//正常方法
(*stu3).age=19;//结构体指针
方法二:指向运算符
stu3 -> age =19;
方法三:
级联访问
stu1.birthday.year = 1998;
(*stu3).birthday.year =1998;
stu3 -> birthday . year =1998;
接上一段参考代码:访问结构体数组的指针
info stu[40]; //info为结构体标签的别名
info *pt;
pt = stu;
stu[0].stuid = 1001;
(*pt).stuid = 1001 ;
pt -> stuid =1001; //三条语句等价
stu[1].stuid =1002 ;
(pt++) -> stuid =1002;
(*pt++).stuid = 1002 ; //语句等价