struct contains a struct-body pointer

Source: Internet
Author: User

Header file:

#include <stdio.h>#include<stdlib.h><string.h>

Global variables:

Const intT_length =2;intS_index = the; typedefstructstudent{intID; Charname[ -]; intAge ;} Student;typedefstructteacher{intID; Charname[ -]; intAge ; intS_num; Student*Stu;} Teacher;

Function Prototypes:

 void  createteahcer (Teacher **ppt,int  t_num); //  void  freeteacher (Teacher *pt, int  t_num); //  free memory  void  sortstudent (Student * pt, int  num); //  Sort the student array  void  sortteacher (Teacher *pt, int  num); //  sort of the teacher array  void  printteacher (Teacher *pt, int  num); //  Iterate through the array  

Implementation method:

voidCreateteahcer (Teacher **ppt,intt_num) {Teacher*temp; Temp= (Teacher *)malloc(sizeof(Teacher) *t_num); *ppt =temp;}voidFreeteacher (Teacher *pt,intt_num) {        inti; if(pt! =NULL) {         for(i =0; i < T_num; ++i) {            if(Pt[i].stu! =NULL) {                 Free(Pt[i].stu); Pt[i].stu=NULL; }        }         Free(PT); }    }voidSortstudent (Student * PT,intnum) {        intI, J;    Student temp;  for(i =0; i < num; ++i) {         for(j = i +1; J < num; ++j) {            if(Pt[j].age <pt[i].age) {Temp=Pt[j]; PT[J]=Pt[i]; Pt[i]=temp; }        }    }}voidSortteacher (Teacher *pt,intnum) {    intI, J;    Teacher temp;  for(i =0; i < num; ++i) {         for(j = i +1; J < num; ++j) {                    if(Pt[j].age <pt[i].age) {Temp=Pt[j]; PT[J]=Pt[i]; Pt[i]=temp;    }} sortstudent (Pt[i].stu, pt[i].s_num); }}voidPrintteacher (Teacher *pt,intnum) {    intI, j =0;    Student Stu; printf ("\ n");  for(i =0; i < num; ++i) {printf ("Study Number:%d Name:%s Age:%d\n", Pt[i].id, Pt[i].name, pt[i].age);  for(j =0; J < Pt[i].s_num; ++j) {Stu=(Pt[i].stu) [j]; printf ("%d students number:%d Name:%s Age:%d\n", J +1, Stu.id, Stu.name, stu.age); }    }}

Test:

intMain () {intI, J; Teacher*PT =NULL; Createteahcer (&pt, T_length);  for(i =0; i < t_length; ++i) {Pt[i].id= i +1002; printf ("Please enter the name of the teacher in section%d:", i +1); scanf ("%s",&(Pt[i].name)); printf ("Please enter the age of the teacher in page%d:", i +1); scanf ("%d", &(Pt[i].age)); printf ("Please enter the number of students who are taking the class%d teachers:", i +1); scanf ("%d", &(Pt[i].s_num)); Pt[i].stu= (Student *)malloc(sizeof(Student) *(Pt[i].s_num)); Allocate memory for students owned by teachers for(j =0; J < Pt[i].s_num; ++j) {(pt[i].stu) [J].id= the+ s_index++; printf ("Please enter the name of the first class%d students:", J +1); scanf ("%s",&((pt[i].stu) [j].name)]; printf ("Please enter the age of%d students:", J +1); scanf ("%d", &((pt[i].stu) [j].age)];    }} sortteacher (Pt,t_length);    Printteacher (PT, t_length);    Freeteacher (PT, t_length); System ("Pause"); return 0;}

Operation Result:

struct contains a struct-body pointer

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.