1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
| #include <stdio.h> #include <stdlib.h> #include <string.h>
typedef struct { char name[50]; int age; float salary; } Employee;
void updateEmployee(Employee *emp, const char *name, int age, float salary) { strcpy(emp->name, name); emp->age = age; emp->salary = salary; }
void printEmployee(const Employee *emp) { printf("姓名:%s,年龄:%d,薪资:%.2f\n", emp->name, emp->age, emp->salary); }
int main() { Employee emp; updateEmployee(&emp, "张三", 28, 8500.0); printEmployee(&emp); Employee *employees = (Employee*)malloc(3 * sizeof(Employee)); if (employees == NULL) { printf("内存分配失败\n"); return 1; } updateEmployee(&employees[0], "李四", 25, 7000.0); updateEmployee(&employees[1], "王五", 30, 9500.0); updateEmployee(&employees[2], "赵六", 35, 12000.0); printf("\n员工列表:\n"); for (int i = 0; i < 3; i++) { printf("%d. ", i + 1); printEmployee(&employees[i]); } free(employees); return 0; }
|