Struct is used as function parameter 2, struct function parameter

Source: Internet
Author: User

Struct is used as function parameter 2, struct function parameter

 

1 # define _ CRT_SECURE_NO_WARNINGS 2 # include <stdio. h> 3 # include <stdlib. h> 4 # include <string. h> 5 6 typedef struct Teacher 7 {8 char name [64]; 9 int age; 10 int id; 11} Teacher; 12 13 void printTeacher (Teacher * array, int num) 14 {15 int I = 0; 16 // print the instructor's age 17 for (I = 0; I <num; I ++) 18 {19 // printf ("age: % d \ n ", array [I]. age); 20 printf ("age: % d \ n", (* (array + I )). age); 21 // (array ++)-> age; 22} 23} // (* (arr Ay + I )). age24 25 void sortTeacher (Teacher * array, int num) 26 {27 int I, j; 28 Teacher tmp; 29 for (I = 0; I <num; I ++) 30 {31 for (j = I; j <num; j ++) 32 {33 if (array [I]. age <array [j]. age) 34 {35 tmp = array [I]; 36 array [I] = array [j]; 37 array [j] = tmp; 38} 39} 40} 41} 42 43 44 Teacher * createTeacher (int num) 45 {46 Teacher * tmp = NULL; 47 tmp = (Teacher *) malloc (sizeof (Teacher) * num); // Teacher Array [3] 48 If (tmp = NULL) 49 {50 return NULL; 51} 52} 53 54 void FreeTeacher (Teacher * p) 55 {56 if (p! = NULL) 57 {58 free (p); 59} 60} 61 62 int main () 63 {64 int I = 0; 65 int num = 3; 66 // Teacher Array [3]; // allocate memory in the stack 67 68 Teacher * pArray = NULL; 69 pArray = createTeacher (num); 70 71 for (I = 0; I <num; I ++) 72 {73 printf ("\ nplease enter age:"); 74 scanf ("% d", & pArray [I]. age); 75} 76 printTeacher (pArray, num); 77 sortTeacher (pArray, num); 78 printf ("sorted: \ n"); 79 printTeacher (pArray, num); 80/* 81 printTeacher (Array, num); 82 sortTeacher (Array, num); 83 printf ("sorted: \ n"); 84 printTeacher (Array, num); */85 FreeTeacher (pArray); 86 system ("pause"); 87 return 0; 88}

 

Related Article

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.