다음은 사용자가 고려할 수있는 예입니다. 이것은 당신이해야 할 일의 템플리트입니다. 직원, 대행사 이름 등을 조작해야합니다. (오류 검사가 없다는 점에 유의하십시오.)
#include <stdio.h>
#include <stdlib.h>
/* TODO: modify members */
typedef struct {
int id;
int salary;
} employee;
/* TODO: modify members */
typedef struct {
char name[20];
employee* emps;
int emps_count;
} agency;
/* return sum of id and salary for first employee from agency */
int dumb_calc(agency ag) {
return ag.emps[0].id + ag.emps[0].salary;
}
int main(void)
{
int num_ag;
int num_emps;
int i, j;
printf("enter num of agencies:\n");
scanf("%d", &num_ag);
agency* agencies = malloc(sizeof(agency) * num_ag);
for (i = 0; i < num_ag; ++i) {
/* TODO: modify single agency name */
sprintf(agencies[i].name, "agency %d", i+1);
printf("enter num of employees for agency %d\n", i+1);
scanf("%d", &num_emps);
agencies[i].emps = malloc(sizeof(employee) * num_emps);
agencies[i].emps_count = num_emps;
for (j = 0; j < num_emps; ++j) {
/* TODO: modify single employee */
agencies[i].emps[j].id = j+1;
agencies[i].emps[j].salary = 1000*(j+1);
}
}
/* TODO: change printing style */
for (i = 0; i < num_ag; ++i) {
printf("agency name: %s\n", agencies[i].name);
printf("num of employees: %d\n", agencies[i].emps_count);
/* result will always be the same */
printf("sum of id and salary for 1st emp: %d\n", dumb_calc(agencies[i]));
}
/* remember to free what you've alloc'd */
for (i = 0; i < num_ag; ++i) {
free(agencies[i].emps);
}
free(agencies);
return 0;
}
이전에 'malloc'을 사용 했습니까? – doctorlove
구조체에 메모리를 할당하고 입력 된 값을 저장하십시오. – Gopi