struct init

 

复制代码
  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 } 
复制代码

 

posted @   叕叒双又  阅读(282)  评论(0编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
点击右上角即可分享
微信分享提示