/* struct-array.c */ #include #include #define N 2 typedef struct{ char *name; /* 氏名 */ double height; /* 身長 */ double weight; /* 体重 */ } health; void show(health hl); int main(void) { health h[N]; int i; for(i = 0;i < N;i++) h[i].name = malloc(20*sizeof(char)); for(i = 0;i < N;i++){ printf("%dの氏名は",i); scanf("%s",h[i].name); /* ポインタのため & は不要 */ printf("身長(m)は"); scanf("%lf",&h[i].height); printf("体重(kg)は"); scanf("%lf",&h[i].weight); } for(i = 0;i < N;i++){ show(h[i]); } for(i = 0;i < N;i++) free(h[i].name); return 0; } void show(health hl) { printf("%10s %5.3f %5.1f \n",hl.name, hl.height, hl.weight); }