結構體中含有結構體指標

來源:互聯網
上載者:User

標籤:pre   type   測試   cer   指標   each   free   create   return   

標頭檔:

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

全域變數:

const int t_length = 2;int s_index = 5000;typedef struct Student{    int id;    char name[64];    int age;}Student;typedef struct Teacher{    int id;    char name[64];    int age;    int s_num;    Student *stu;}Teacher;

函數原型:

void createTeahcer(Teacher **ppt,int t_num);//為教師數組分配記憶體void freeTeacher(Teacher *pt, int t_num); //釋放記憶體void sortStudent( Student * pt, int num ); //學生數組的排序void sortTeacher(Teacher *pt, int num); //教師數組的排序void printTeacher(Teacher *pt, int num); //遍曆數組

實現方法:

void createTeahcer(Teacher **ppt,int t_num){    Teacher *temp;    temp = (Teacher *)malloc(sizeof(Teacher) * t_num);    *ppt = temp;}void freeTeacher(Teacher *pt, int t_num){        int i;    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);    }    }void sortStudent( Student * pt, int num ) {        int i, 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;            }        }    }}void sortTeacher(Teacher *pt, int num){    int i, 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);    }}void printTeacher(Teacher *pt, int num){    int i, j = 0;    Student stu;    printf("\n");    for(i = 0; i < num; ++i){        printf("學號:%d 姓名:%s 年齡:%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位學生 學號:%d 姓名:%s 年齡:%d\n",j + 1, stu.id, stu.name, stu.age);        }    }}

測試:

int main(){    int i, j;    Teacher *pt = NULL;    createTeahcer(&pt, t_length);    for(i = 0; i < t_length; ++i){        pt[i].id = i + 1002;        printf("請輸入第%d老師位的名字:", i + 1);        scanf("%s",&(pt[i].name));        printf("請輸入第%d位老師的年齡:", i + 1);        scanf("%d", &(pt[i].age));                printf("請輸入第%d位老師的所帶學生的人數:", i + 1);        scanf("%d", &(pt[i].s_num));        pt[i].stu = (Student *)malloc(sizeof(Student) * (pt[i].s_num)); //為老師所擁有的學生分配記憶體        for( j = 0; j < pt[i].s_num; ++j){            (pt[i].stu)[j].id = 5000 + s_index++;            printf("請輸入第%d位同學的名字:", j + 1);            scanf("%s",&((pt[i].stu)[j].name));            printf("請輸入第%d位同學的年齡:", 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;}

運行結果:

 

結構體中含有結構體指標

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.