c的详细学习(9)结构体与共用体的学习(一)
2016-04-19 09:54
274 查看
C语言提供了另外两种构造类型:结构体与公用体,用来存储若干个类型不同但彼此组成一个集合的数据总体。
(1)结构体类型与结构体变量
1.定义
其一般形式为:
struct 结构体类型名{
数据类型1 成员名1;
数据类型2 成员名2;
数据类型3 成员名3;
......
}
定义一个结构体变量只是描述结构体的组织形式,并不意味着将分配一段内存单元来存放个数据项成员。它的作用只是告诉编译系统所定义的结构体类型是有哪些类型的成员构成的,各占多少字节,按什么形式存储,并把他们当作一个整体来处理。
2.结构体变量的定义:
结构体类型的变量的定义有三种方法:
1)先定义结构体类型,再定义结构体变量;struct student{省略成员的定义};struct student stu1,stu2;
2)定义结构体类型的同时,定义结构体变量;struct student{省略成员的定义}stu1,stu2;
3)不定义结构体的类型,直接定义变量。(此时结构体是无名的)struct{省略成员的定义}stu1,stu2;
结构体成员也可以是一个结构体变量,即一个结构体的定义中可以嵌套另外一个结构体的结构体的结构体。
struct date{
int year;
int month;
int day;
};
struct student{
long int number;
char name[20];
char sex;
struct date birthday;
char addr[30];
};
3.结构体变量的引用
在定义结构体类型变量以后,就可以引用结构体类型变量,如赋值,存取和运算等。遵循以下规则:
1)不能将结构体变量当作一个整体处理;
2)访问带有结构体变量成员的结构体时应采取逐级访问的方式;
3)结构体的成员变量和普通变量一样可以进行各种运算。
4.结构体变量的初始化
结构体类型是数组类型的扩充,只是它的成员项可以具有不同的数据类型:
struct student{
long num;
char name[20];
char sex;
char addr[20];
}s1={2006001,"MenAngel",'m',"anhui hefei"};
(2)结构体数组
结构体数组与普通的变量的数组大致相同:
初始化:
struct student{}stu1={{括号里与单个结构体初始化方式相同},{},{}......};
1)输入3名学生的信息并输出
本例用是scanf函数输入各成员时,除字符型数组外,其他变量都采用","分隔,而字符数组以回车作为输入的结束。
2)统计候选人得票数。假设有3名候选人,每次输入一个得票人的名字,要求最后输出每个人的得票总数。
(3)结构体指针
指向结构体的指针称为结构体指针变量。该变量存放结构体变量的起始地址。结构体指针变量也可以指向结构体数组中的元素。
1.结构体指针变量的定义
struct 结构体类型 *结构体指针
例如:
struct student stu1,*p=&stu1;
在访问结构体变量的成员时:p->num等价于(*p).num;
2.结构体数组指针
一个结构体指针变量可以指向结构体数组,即将结构体数组的起始地址赋给指针变量,这种指针就是结构体指针。
struct student stu1[10],*p=stu1;
(4)结构体类型数据在函数间的传递
函数间不仅可以传递简单变量、数组、指针这些类型的数据,还可以传递结构体类型的数据。函数间结构体类型数据的传递和普通变量一样,可以“按值传递”,也可以“按地址传递”。
1.结构体变量作为函数参数
用结构体变量作为函数实参传递数据:
调用函数的实参与被调用函数的形参都是结构体变量名。
形参和实参的结构体类型相同,但运行时分配在不同的存储空间,因此,被调用函数不能修改调用函数的值。
2.结构体指针变量作为函数参数
3.结构体数组作为函数参数
用结构体数组写一个联系人管理的c程序,包括增删改查,打印所有联系人信息五个功能!
由于屏幕小,测试功能的截屏就是以上那么多!
总结遇到的问题:
1.在从键盘输入数据用scanf("%d",n),忘了加&,然而也不报错,检查半天,痛心!应为scanf("%d",&n);
2.本例用记录数组大小的数count,和结构体数组作为全局变量,事实上这样在每个函数中都可以自由更改它们的值。如果使用值传递的方式是不能更改count的值的,也不能更改结构体数组的值,操作起来非常不方便。
3.在用gets()函数,或者scanf()函数接收字符串时一定要确定前面是否有scanf()函数,如果有,确定用getchar();接收上面输入过程产生的回车符。
4.本例用long型数据存储电话号码,但貌似只能有8位整数,unsigned long貌似只能输入9位,无法达到11位的要求。这里就不在详细追究。事实上可以用字符数组来接收电话号码。
(1)结构体类型与结构体变量
1.定义
其一般形式为:
struct 结构体类型名{
数据类型1 成员名1;
数据类型2 成员名2;
数据类型3 成员名3;
......
}
定义一个结构体变量只是描述结构体的组织形式,并不意味着将分配一段内存单元来存放个数据项成员。它的作用只是告诉编译系统所定义的结构体类型是有哪些类型的成员构成的,各占多少字节,按什么形式存储,并把他们当作一个整体来处理。
2.结构体变量的定义:
结构体类型的变量的定义有三种方法:
1)先定义结构体类型,再定义结构体变量;struct student{省略成员的定义};struct student stu1,stu2;
2)定义结构体类型的同时,定义结构体变量;struct student{省略成员的定义}stu1,stu2;
3)不定义结构体的类型,直接定义变量。(此时结构体是无名的)struct{省略成员的定义}stu1,stu2;
结构体成员也可以是一个结构体变量,即一个结构体的定义中可以嵌套另外一个结构体的结构体的结构体。
struct date{
int year;
int month;
int day;
};
struct student{
long int number;
char name[20];
char sex;
struct date birthday;
char addr[30];
};
3.结构体变量的引用
在定义结构体类型变量以后,就可以引用结构体类型变量,如赋值,存取和运算等。遵循以下规则:
1)不能将结构体变量当作一个整体处理;
2)访问带有结构体变量成员的结构体时应采取逐级访问的方式;
3)结构体的成员变量和普通变量一样可以进行各种运算。
4.结构体变量的初始化
结构体类型是数组类型的扩充,只是它的成员项可以具有不同的数据类型:
struct student{
long num;
char name[20];
char sex;
char addr[20];
}s1={2006001,"MenAngel",'m',"anhui hefei"};
(2)结构体数组
结构体数组与普通的变量的数组大致相同:
初始化:
struct student{}stu1={{括号里与单个结构体初始化方式相同},{},{}......};
1)输入3名学生的信息并输出
#include<stdio.h> #include<string.h> struct student{ long num; char name[20]; int age; char sex; int score; }; int main(){ struct student stu[3]; int i; printf("学生的数据是:长整型的学号,字符数组的名字,整型的年龄,字符型的性别,整型的分数。\n"); for(i=0;i<3;i++){ printf("Please input all data of student[%d]\n",i+1); scanf("%ld,%s%d,%c,%d",&stu[i].num,&stu[i].name,&stu[i].age,&stu[i].sex,&stu[i].score); } printf("\n num\t name\t age\t sex\t score\n"); for(i=0;i<3;i++){ printf("%ld\t%s\t%d\t%4c\t%5d\n",stu[i].num,stu[i].name,stu[i].age,stu[i].sex,stu[i].score); } return 0; }
本例用是scanf函数输入各成员时,除字符型数组外,其他变量都采用","分隔,而字符数组以回车作为输入的结束。
2)统计候选人得票数。假设有3名候选人,每次输入一个得票人的名字,要求最后输出每个人的得票总数。
#include<stdio.h> #include<string.h> struct person { char name[20]; int count; } leader[3]={"Hu",0,"Li",0,"Ma",0};//结构体数组的赋值 int main(){ int i,j; char leader_name[20]; for(i=1;i<=5;i++){ scanf("%s",leader_name); for(j=0;j<3;j++){ if(strcmp(leader_name ,leader[j].name )==0){ leader[j].count++; } } } for(i=0;i<3;i++){ printf("%5s:%d\n",leader[i].name,leader[i].count); } return 0; }
(3)结构体指针
指向结构体的指针称为结构体指针变量。该变量存放结构体变量的起始地址。结构体指针变量也可以指向结构体数组中的元素。
1.结构体指针变量的定义
struct 结构体类型 *结构体指针
例如:
struct student stu1,*p=&stu1;
在访问结构体变量的成员时:p->num等价于(*p).num;
2.结构体数组指针
一个结构体指针变量可以指向结构体数组,即将结构体数组的起始地址赋给指针变量,这种指针就是结构体指针。
struct student stu1[10],*p=stu1;
(4)结构体类型数据在函数间的传递
函数间不仅可以传递简单变量、数组、指针这些类型的数据,还可以传递结构体类型的数据。函数间结构体类型数据的传递和普通变量一样,可以“按值传递”,也可以“按地址传递”。
1.结构体变量作为函数参数
用结构体变量作为函数实参传递数据:
#include<stdio.h> struct Teacher{ char name[20]; float salary; float reword; float income; }; void display(struct Teacher p) { printf("\n%s %7.2f %7.2f %7.2f",p.name,p.salary,p.reword,p.income); p.salary=3000; p.reword=2000; p.income=p.salary+p.reword; printf("\n%s %7.2f %7.2f %7.2f",p.name,p.salary,p.reword,p.income); } int main(){ struct Teacher teacher; printf("\nInput name:"); scanf("%s",teacher.name); teacher.salary=2000; teacher.reword=1000; teacher.income=teacher.salary+teacher.reword; display(teacher); printf("\n%s %7.2f %7.2f %7.2f",teacher.name,teacher.salary,teacher.reword,teacher.income); }
调用函数的实参与被调用函数的形参都是结构体变量名。
形参和实参的结构体类型相同,但运行时分配在不同的存储空间,因此,被调用函数不能修改调用函数的值。
2.结构体指针变量作为函数参数
#include<stdio.h> struct student{ long num; char name[12]; float score[3]; }stb1={2006001,"LiYing",67.9,78.5,91.2},stb2={2006002,"YangLi",87.9,97.6,90.0}; void output(struct student *p){ printf("%ld\n%s\n%f\n%f\n%f\n",p->num,p->name,p->score[0],p->score[1],p->score[2]); printf("\n"); }; int main(){ output(&stb1); output(&stb2); return 0; }
3.结构体数组作为函数参数
用结构体数组写一个联系人管理的c程序,包括增删改查,打印所有联系人信息五个功能!
#include<stdio.h> #include<stdlib.h> #include<string.h> struct contacter{ int num; char name[15]; unsigned long linknumber; }; struct contacter linkers[100]; int count=0; //程序开始时的提醒: void FirstWarn(){ printf("This is a c program used to manange your address book:\n"); printf("Chose different choice,then you will get disfferent function:\n"); printf("1.Establish and save a new contacter:\n"); printf("2.Delete a contacter:\n"); printf("3.Modify a contacter:\n"); printf("4.Search information of a contacter:\n"); printf("5.Print all of your contacters:\n"); } //第1个功能,用来新建一个联系人 void newContacters(){ getchar(); if(count==100){ printf("There is no storage space any more!\n"); }else if(count>=0&&count<100){ printf("Please input name:\n"); gets(linkers[count].name); printf("please input his or her telephone number:\n"); scanf("%ld",&linkers[count].linknumber); linkers[count].num=14001+count; count++; } } //用来验证输入的命令数字 void validate(int* p){ if(*p<1|*p>5){ printf("Warning: No Such order!\n"); printf("Please input a new order again:\n"); scanf("%d",p); validate(p); } } //第2个功能,实现删除一个联系人 //(1)用号码来删除 void deleteByNumber(int num){ int n; n=num-14001; for(int i=n;i<count;i++){ strcpy(linkers[i].name,linkers[i+1].name); linkers[i].linknumber=linkers[i+1].linknumber; } count--; } //(2)用名字来删除 void deleteByName(char name[]){ for(int i=0;i<count;i++) if(!strcmp(name,linkers[i].name)) deleteByNumber(14001+i); } //第3个功能,实现输入名称修改联系人的信息 void modifyByName(char *name){ for(int i=0;i<count;i++){ if(!strcmp(name,linkers[i].name)){ printf("Please input this linker's telephone:\n"); scanf("%d",&linkers[i].linknumber); } printf("After modifing:num=%d name=%s telephone=%ld\n", linkers[i].num,linkers[i].name,linkers[i].linknumber); } } void modifyByNumber(int num){ printf("Please input this linker's name\n"); gets(linkers[num-14001].name); printf("Please input this linkers' telephone\n"); scanf("%ld",&linkers[num-14001].linknumber); printf("After modifing:num=%d name=%s telephone=%ld\n", linkers[num-14001].num,linkers[num-14001].name,linkers[num-14001].linknumber); } //第4个功能,实现查询功能 int searchByName(char *name,struct contacter *p,int count){ for(int i=0;i<count;i++){ if(!strcmp(name,p[i].name)){ printf("The information of this person:\n"); printf("num=%d,name=%15s,telephone=%ld\n",p[i].num,p[i].name,p[i].linknumber); return 1; } } return 0; } int searchByNumber(int num,struct contacter *p, int count){ if(0<=num-14001&&num-14001<=count){ printf("num=%d,name=%15s,telephone=%ld\n", p[num-14001].num,p[num-14001].name,p[num-14001].linknumber); } } //第5个功能,实现打印信息 void printAll(){ for(int i=0;i<count;i++){ printf("第%d位联系人:num=%d; name=%15s; telephone=%ld \n",i+1,linkers[i].num,linkers[i].name,linkers[i].linknumber); } } int main(){ //在启动程序时自动创建一个包含100个变量的结构体数组 //记录用户的选择 int choice; FirstWarn(); while(1){ printf("Please input a order:\n"); scanf("%d",&choice); validate(&choice); switch(choice){ case 1:{ newContacters(); break; } case 2:{ printf("Chose the way of deleting linkers:\n"); printf("0.By name\n"); printf("1.By Number:\n"); int temp; scanf("%d",&temp); if(temp){ int number; printf("Input the number:\n"); scanf("%d",&number); deleteByNumber(number); }else{ char name[10]; printf("Print the name of linker who you want to delete:\n"); scanf("%s",name); deleteByName(name); } break; } case 3:{ int list=0; printf("Chose a way to modyfy the contacter:\n"); printf("1.Modify the telephone only(By name)!\n"); printf("0.Modify both name and telephone(By number)!\n"); scanf("%d",&list); if(list){ char name[10]; printf("Please input the name:\n"); getchar(); gets(name); modifyByName(name); }else{ int num; printf("Please input the number:\n"); scanf("%d",&num); getchar(); modifyByNumber(num); } break; } case 4: { int list=0; printf("Chose a way to search the contacter:\n"); printf("1.search by name!\n"); printf("0.search by number!\n"); scanf("%d",&list); if(list){ char name[10]; printf("Please input the name:\n"); scanf("%s",name); if(!searchByName(name,linkers,count)){ printf("no such man!"); } }else{ int num; printf("Please input the person's number you want to search:\n"); scanf("%d",&num); searchByNumber(num,linkers,count); } break; } case 5: {printAll(); break;} default:printf("Errors!");break; } } }
由于屏幕小,测试功能的截屏就是以上那么多!
总结遇到的问题:
1.在从键盘输入数据用scanf("%d",n),忘了加&,然而也不报错,检查半天,痛心!应为scanf("%d",&n);
2.本例用记录数组大小的数count,和结构体数组作为全局变量,事实上这样在每个函数中都可以自由更改它们的值。如果使用值传递的方式是不能更改count的值的,也不能更改结构体数组的值,操作起来非常不方便。
3.在用gets()函数,或者scanf()函数接收字符串时一定要确定前面是否有scanf()函数,如果有,确定用getchar();接收上面输入过程产生的回车符。
4.本例用long型数据存储电话号码,但貌似只能有8位整数,unsigned long貌似只能输入9位,无法达到11位的要求。这里就不在详细追究。事实上可以用字符数组来接收电话号码。
相关文章推荐
- Android Message和obtainMessage的区别
- BZOJ 3676 UOJ 103 APIO 2014 后缀自动机 Manacher
- yum安装mysqsl登陆失败
- 函数、事件全紫色、紫白色、全白色是啥意思
- mysql索引解析
- session原理总结
- matlab 曲线线型、颜色和标记点 和 legend
- VC 抓屏的各种方法
- python默认编码问题
- 计算机安装双系统后系统引导修复的方法
- selenium配置
- (转)JS保留两位小数 四舍五入函数
- LeetCode-120.Triangle
- iOS开发之离线地图核心代码
- Android Studio 2.0 Instant Run问题解决方法
- linux下nfs共享文件
- 怎么控制表单placeholder属性的样式兼容各大浏览器?
- Rxlifecycle使用详解,解决RxJava内存泄露问题
- Python学习笔记(八)——有关文件的操作
- cocos2dx 安卓makefile Android.mk,动态引用cpp地址