C语言中的结构体指针
来源:互联网 发布:合肥办公软件培训学校 编辑:程序博客网 时间:2024/06/12 19:24
struct Birthday{ int year; int month; int day;};//声明结构体类型//typedef:定义新类型,为类型取别名//typedef 原有类型,新类型typedef struct Student{ char *name; int age; int code; double score[3]; //嵌套 struct Birthday birthday;} Student;void printStudent(struct Student student);void printStudents(struct Student *student,int length );void soreStudents(Student *student,int length);void exchangeAge(Student *student,Student *anotherstudent);int main(int argc,const char * argv[]){ //定义结构体数组 struct Student students[5] ={ {"student1",21,1,{80,99,90},{1993, 10, 3}}, {"student2",20,2,{80,99,90},{1993, 10, 3}}, {"student3",23,3,{80,99,90},{1993, 10, 3}}, {"student4",21,4,{80,99,90},{1993, 10, 3}}, {"student5",26,5,{80,99,90},{1993, 10, 3}}}; Student *student=&students[0]; Student *anotherstudent=&student[1]; printStudent(*student); printStudent(*anotherstudent); printf("年龄改变了!!!\n"); exchangeAge(student, anotherstudent); printStudent(*student); printStudent(*anotherstudent); return 0;}//打印出单个学生的信息void printStudent(struct Student student){ printf("<Student : %p> name =%s age =%d code = %d score = {%.2f,%.2f,%.2f} birthday = %d.%d.%d\n", &student, student.name, student.age, student.code, student.score[0], student.score[1], student.score[2], student.birthday.year, student.birthday.month, student.birthday.day); //符号写错毁一生啊,里面逗号打成分号了啊。。。}//打印出所有学生的信息void printStudents(struct Student *student,int length ){ if (student == NULL && length == 0) { return; } for (int i = 0; i < length; i++) { printStudent(student[i]); }}//数组的排序(根据学生的年龄)void soreStudents(Student *student,int length){ if (student == NULL || length == 0) { return; } for (int i =0; i < length; i++) { for (int j = i; j<length; j++) { if (student[i].age>student[j].age) { Student temp = student[i]; student[i] = student[j]; student[j] = temp; } } }}void exchangeAge(Student *student,Student *anotherstudent){ if(student == NULL || anotherstudent == NULL ){ return; } //第一种方法// int temp = student -> age;// student -> age=anotherstudent -> age;// anotherstudent -> age = temp; //第二种方法 int temp = (*student).age; (*student).age = (*anotherstudent).age; (*anotherstudent).age = temp;}
输出结果:
<Student : 0x7fff5fbff6d0> name =student1 age =21 code = 1 score = {80.00,99.00,90.00} birthday = 1993.10.3
<Student : 0x7fff5fbff6d0> name =student2 age =20 code = 2 score = {80.00,99.00,90.00} birthday = 1993.10.3
年龄改变了!!!
<Student : 0x7fff5fbff6d0> name =student1 age =20 code = 1 score = {80.00,99.00,90.00} birthday = 1993.10.3
<Student : 0x7fff5fbff6d0> name =student2 age =21 code = 2 score = {80.00,99.00,90.00} birthday = 1993.10.3
Program ended with exit code: 0
0 0
- C语言--结构体中的指针
- C语言--结构体中的指针
- C语言--结构体中的指针
- C语言--结构体中的指针
- C语言中的结构体指针
- C语言中的结构体指针
- C语言结构体中的函数指针
- C语言中的结构体指针
- C语言结构体中的函数指针与函数
- c语言结构体中的函数指针与函数
- C语言结构体指针在函数调用中的使用
- c语言结构体指针
- C语言---结构体指针
- 【C语言】结构体指针
- C语言指针-----指针与结构体
- C语言 结构体的应用 查询结构体中的信息 结构体与指针操作
- 【C】 结构体与结构体指针,->在C语言中的唯一用途
- C语言结构指针
- VMware简介、它的特点以及在里面如何创建快照、克隆
- camera 3 hal 不支持 soc
- 算法之二叉树各种遍历
- autolayout
- 博士后和民工的区别
- C语言中的结构体指针
- Android设置标题栏后,关于Toast异常的解决办法
- Swap Nodes in Pairs
- iOS8下的开发变化
- php学习笔记(二十七)读取session和删除session
- 页面元素隐藏和显示
- Linux上iptables防火墙的基本应用教程
- iOS 清理缓存代码
- Linux之Makefile(dir)