示例代码:
- #include<stdio.h> //头文件
- //结构体,嵌套结构体
- struct student{ //结构体 struct关键字修饰
- int number; //学号
- char name[20]; //姓名
- char sex; //性别
- struct date{ //结构体 日期 嵌套结构体
- int year; //年
- int month; //月
- int day; //日
- }birthday;
- float score; //总分
- char speciality[11]; //特长
- char school[31]; //学校
- };
- main(){
- //结构体变量的初始化
- struct student st1={1001,"Liming",'0',1983,10,25,573,"Computer","3th High School"
- };
- printf("%4d %-10s %c %4d %2d %2d %.2f %-10s %s\n",
- st1.number,st1.name,st1.sex,
- st1.birthday.year,st1.birthday.month,st1.birthday.day,
- st1.score,st1.speciality,st1.school
- );
- ---------------------------------------------------------------------------------------------------------------------------
- //结构体变量的引用
- struct student st2;
- st2.number=1002;
- strcpy(st2.name,"Zhangsan");
- str2.sex='f';
- str2.birthday.year=1919;
- str2.birthday.month=10;
- str2.birthday.day=1;
- str2.score=666;
- str2.speciality="Car";
- str2.school="1th High School";
- -----------------------------------------------------------------------------------------------------------------------------------
- //结构体数组
- int i;
- struct student st[2]={
- {
- "11","Liming",'0',1983,10,25,573,"Computer","3th High School"
- },
- {
- "12","WangLi",'1',1984,2,16,590,"Physics","Remin Middle School"
- }
- };
- for(i=0;i<2;i++) printf("\n%4s %-10s %c %4d %2d %2d %.2f %-10s %s\n",
- st[i].number,st[i].name,st[i].sex,
- st[i].birthday.year,st[i].birthday.month,st[i].birthday.day,
- st[i].score,st[i].speciality,st[i].school
- );
- }
- ---------------------------------------------------------------------------------------------------------------
- //结构体数组初始化
- struct student{
- int num;
- char name[20];
- char sex;
- float score;
- }stu[3]={
- {11,"Liang tao",'t',80},
- {13,"Zhao jun",'t',97.5},
- {14,"Hai kang",'f',58}
- };
- ---------------------------------------------------------------------------------------------------------------
- //结构体指针
- struct student{ //结构体student
- int num;
- char name[20];
- char sex;
- float score;
- };
- struct student stuone; //结构体变量stuone
- struct student *p; //结构体指针 *p
- p=&stuone; //结构体指针指向结构体变量
- stuone.num=10871; //结构体变量初始化
- strcpy(stuone.name,"Mary");
- stuone.sex='F';
- stuone.score=98.0;
- //直接输出结构体变量的值
- printf("num:%d\nname:%s\nsex:%c\nscore:%.2f\n",
- stuone.num,stuone.name,stuone.sex,stuone.score
- );
- //通过结构体指针输出结构体变量的值 方法1
- printf("num:%d\nname:%s\nsex:%c\nscore:%.2f\n",
- (*p).num,(*p).name,(*p).sex,(*p).score
- );
- //通过结构体指针输出结构体变量的值 方法2
- printf("num:%d\nname:%s\nsex:%c\nscore:%.2f\n",
- p->num,p->name,p->sex,p->score
- );
- -------------------------------------------------------------------------------------
- //结构体变量指针作为参数
- struct student{
- int num;
- char name[20];
- int score[3];
- }stu={10020,"Mary",89,98,79}; //结构体变量stu,初始化结构体变量
- void print(struct student *p){ //print()函数,传入结构体变量的指针
- printf("%d %s %d %d %d",p->num,p->name,p->score[0],p->score[1],p->score[2]);//输出指针所指信息
- printf("\n");
- }
- main(){
- print(&stu); //调用print()函数
- }
- --------------------------------------------------------------------------------------------
- //结构体型的函数
- struct person{
- int num;
- char name[20];
- char telephone[12];
- char email[40];
- char addr[30];
- }con_person[3]={ //结构体数组
- {9,"Zhangli","13817890789","zhangli@263.net","Shanghai"},
- {6,"Jack","01089789091","jack09@sin.com","Beijing"},
- {12,"Qiuzhi","13762457986","qiuzhi@sohu.com","Hangzhou"}
- };
- struct person find(int n){ //结构体函数find()
- int i;
- for(i=0;con_person[i].num!=0;i++){
- if(con_person[i].num==n)
- return(con_person[i]);
- }
- }
- main(){
- int number;
- struct person result; //结构体变量
- printf("Enter the number:");
- scanf("%d",&number); //输入编号
- result=find(number); //调用结构体函数find()
- if(result.num!=0){
- printf("Name:%s\n",result.name);
- printf("Telephone:%s\n",result.telephone);
- printf("Email:%s\n",result.email);
- printf("Address:%s\n",result.addr);
- }else{
- printf("Not foutd!\n");
- }
- }
- ----------------------------------------------------------------------------------------------
- //结构体指针函数
- struct person *find(int n){ //结构体指针型函数find()
- int i;
- for(i=0;con_person[i].num!=0;i++){
- if(con_person[i].num==n)
- return(&con_person[i]);
- }
- }
- main(){
- int number;
- struct person *result; //结构体指针型变量
- printf("Enter the number:");
- scanf("%d",&number); //输入编号
- result=find(number); //调用结构体指针型函数*find()
- if(result->num!=0){
- printf("Name:%s\n",result->name);
- printf("Telephone:%s\n",result->telephone);
- printf("Email:%s\n",result->email);
- printf("Address:%s\n",result->addr);
- }else{
- printf("Not found!\n");
- }
- }