-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathstruct.c
More file actions
58 lines (49 loc) · 1.52 KB
/
struct.c
File metadata and controls
58 lines (49 loc) · 1.52 KB
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
50
51
52
53
54
55
56
57
58
/**
* Copyright © https://github.com/microwind All rights reserved.
* @author: jarryli@gmail.com
* @version: 1.0
* @description: 结构体数据结构 - C实现
*/
#include <stdio.h>
#include <string.h>
// Person 结构体:姓名、年龄、地址
struct Person {
char name[50];
int age;
char address[100];
};
// Employee 结构体:组合 Person 成员,加职位
struct Employee {
struct Person person; // 内嵌 Person
char position[50];
};
// 初始化 Employee(同时初始化内部的 Person)
void initEmployee(struct Employee *e, const char *name, int age, const char *address, const char *position) {
strcpy(e->person.name, name);
e->person.age = age;
strcpy(e->person.address, address);
strcpy(e->position, position);
}
// 显示员工信息
void introduceEmployee(struct Employee *e) {
printf("I am %s, a %s at the company, living in %s.\n",
e->person.name, e->position, e->person.address);
}
int main() {
// Person 示例
struct Person p1;
strcpy(p1.name, "Alice");
p1.age = 30;
strcpy(p1.address, "123 Main St");
printf("Hi, I am %s, %d years old, from %s.\n", p1.name, p1.age, p1.address);
// Employee 示例
struct Employee e1;
initEmployee(&e1, "Jarry", 28, "456 Elm St", "Software Developer");
introduceEmployee(&e1);
return 0;
}
/*打印结果
jarry@Mac struct % gcc struct.c -o struct && ./struct
Hi, I am Alice, 30 years old, from 123 Main St.
I am Jarry, a Software Developer at the company, living in 456 Elm St.
*/