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

时间:2023-03-09 17:29:23
C/C++ 结构体 指针 函数传递
 #include <stdio.h>
#include <stdlib.h> struct student{
int num;
char str[];
double dec;
}; void scan(struct student *stu[], int *n){
scanf("%d", n);
*stu = (struct student *)malloc(*n * sizeof(struct student)); for(int i = ; i < *n; ++i){
scanf("%d%s%lf", &(*stu)[i].num, (*stu)[i].str, &(*stu)[i].dec);
}
} void print(struct student stu[], int n){ printf("%d\n", n);
for(int i = ; i < n; ++i){
printf("%d %s %lf\n", stu[i].num, stu[i].str, stu[i].dec);
}
} int main(){
int n;
struct student *stu; scan(&stu, &n);
print(stu, n); free(stu);
return ;
}
/*
3
20 字符串0 20.02
21 字符串1 21.12
22 字符串2 22.22
*/