• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
爱白菜的小昆虫
博客园    首页    新随笔    联系   管理    订阅  订阅

C/C++ 结构体 指针 函数传递

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 
 4 struct student{
 5     int num;
 6     char str[20];
 7     double dec;
 8 };
 9 
10 void scan(struct student *stu[], int *n){    
11     scanf("%d", n);
12     *stu = (struct student *)malloc(*n * sizeof(struct student));
13 
14     for(int i = 0; i < *n; ++i){
15         scanf("%d%s%lf", &(*stu)[i].num, (*stu)[i].str, &(*stu)[i].dec);
16     }
17 }
18 
19 void print(struct student stu[], int n){    
20 
21     printf("%d\n", n);
22     for(int i = 0; i < n; ++i){
23         printf("%d %s %lf\n", stu[i].num, stu[i].str, stu[i].dec);
24     }
25 }
26 
27 int main(){
28     int n;
29     struct student *stu;
30     
31     scan(&stu, &n);
32     print(stu, n);
33 
34     free(stu);
35     return 0;
36 }
37 /*
38 3
39 20 字符串0 20.02
40 21 字符串1 21.12
41 22 字符串2 22.22
42 */

 

posted @ 2016-12-11 18:12  爱白菜的小昆虫  阅读(752)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3