zl程序教程

您现在的位置是:首页 >  其它

当前栏目

struct init

init struct
2023-09-11 14:22:21 时间

 

  1 #include <stdio.h>
  2 #include <stdlib.h>
  3 #include <string.h>
  4 #include <stdbool.h>
  5 #define SIZE 5
  6 
  7 typedef struct _student{
  8     char *name;
  9     int age;
 10     short id;
 11     double record;
 12 } Student;
 13 
 14 Student *initStudent(){
 15     Student *ptrStu = (Student *)malloc(sizeof(Student));
 16     ptrStu->name = (char *)malloc(sizeof(char) * 20);
 17     printf("please input student info: ");
 18     printf("\nname: ");
 19     scanf("%s", ptrStu->name);
 20     printf("age: ");
 21     scanf("%d", &ptrStu->age);
 22     printf("id: ");
 23     scanf("%d", &ptrStu->id);
 24     printf("record: ");
 25     scanf("%f", &ptrStu->record);
 26 
 27     return ptrStu;
 28 }
 29 
 30 void displayStudent(Student *ptrStu){
 31         printf("The student %s's info: \n", ptrStu->name);
 32         printf("name: %s\n", ptrStu->name);
 33         printf("age: %d\n", ptrStu->age);
 34         printf("id: %d\n", ptrStu->id);
 35         printf("record: %f\n", ptrStu->record);
 36     printf("\n");
 37 
 38     return;
 39 }
 40 
 41 int main(int argc, char **argv)
 42 {
 43     Student *arrStu[SIZE];
 44     for(int i = 0; i < SIZE; i++){
 45         arrStu[i] = initStudent();
 46     }
 47     for(int i = 0; i < SIZE; i++){
 48         displayStudent(arrStu[i]);
 49     }
 50 
 51     return 0;
 52 }