#include <stdio.h>
//结构体关键字 结构体标签
struct student
//结构体标签——改为:typedef srtuct student,那么可以再本}结尾重命名stu,引用直接用这个名字也可
{
char name[20];//成员变量,里面也可以放其他结构体
short age;
char id[20];
}s1,s2,s3;//这三个是全局的结构体变量,但是代码中老师建议尽量少用全局变量(上面———>重命名stu;)
int main()
{
struct student s4={"张三",20,"2202047"};//这个是局部的结构体变量——这里可以用stu s4即可
//打印
printf("%s\n",s4.name);//点操作符来访问
return 0;
}
用点操作符,结构体变量 . 成员
#include <stdio.h>
struct student
{
char name[20];//成员变量
short age;
char id[20];
};
void print(struct student* s)
{
printf("%s\n",s->name);
}
int main()
{
struct student s4={"张三",20,"123456"};
print(&s4);//传地址
return 0;
}
用箭头操作符,结构体指针->成员
更好的用传结构体地址