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 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
| #include <stdio.h> #include <string.h> #include <stdbool.h>
struct Student { int id; char name[50]; int age; float gpa; bool is_graduate; };
typedef struct { double x, y; char color[20]; float radius; } Circle;
typedef struct { int year; int month; int day; } Date;
typedef struct { char title[100]; char author[50]; Date publish_date; float price; int pages; } Book;
void demonstrate_struct_basics(void) { printf("=== 结构体基础演示 ===\n"); struct Student student1; student1.id = 1001; strcpy(student1.name, "张三"); student1.age = 20; student1.gpa = 3.8f; student1.is_graduate = false; struct Student student2 = {1002, "李四", 21, 3.6f, false}; struct Student student3 = { .id = 1003, .name = "王五", .age = 22, .gpa = 3.9f, .is_graduate = true }; struct Student student4 = {1004, "赵六"}; printf("学生信息:\n"); printf("学生1: ID=%d, 姓名=%s, 年龄=%d, GPA=%.1f, 毕业=%s\n", student1.id, student1.name, student1.age, student1.gpa, student1.is_graduate ? "是" : "否"); printf("学生2: ID=%d, 姓名=%s, 年龄=%d, GPA=%.1f, 毕业=%s\n", student2.id, student2.name, student2.age, student2.gpa, student2.is_graduate ? "是" : "否"); printf("学生3: ID=%d, 姓名=%s, 年龄=%d, GPA=%.1f, 毕业=%s\n", student3.id, student3.name, student3.age, student3.gpa, student3.is_graduate ? "是" : "否"); printf("学生4: ID=%d, 姓名=%s, 年龄=%d, GPA=%.1f, 毕业=%s\n", student4.id, student4.name, student4.age, student4.gpa, student4.is_graduate ? "是" : "否"); printf("\n嵌套结构体演示:\n"); Book book = { .title = "C语言程序设计", .author = "谭浩强", .publish_date = {2020, 3, 15}, .price = 45.50f, .pages = 380 }; printf("书籍信息:\n"); printf("书名: %s\n", book.title); printf("作者: %s\n", book.author); printf("出版日期: %d年%d月%d日\n", book.publish_date.year, book.publish_date.month, book.publish_date.day); printf("价格: %.2f元\n", book.price); printf("页数: %d页\n", book.pages); }
void print_student_by_value(struct Student s) { printf("按值传递 - 学生信息: ID=%d, 姓名=%s\n", s.id, s.name); s.id = 9999; }
void print_student_by_pointer(const struct Student* s) { if (s == NULL) return; printf("按指针传递 - 学生信息: ID=%d, 姓名=%s\n", s->id, s->name); }
void update_student_gpa(struct Student* s, float new_gpa) { if (s != NULL) { s->gpa = new_gpa; } }
void demonstrate_struct_parameters(void) { printf("\n=== 结构体参数传递 ===\n"); struct Student student = {1001, "测试学生", 20, 3.5f, false}; printf("原始学生信息: ID=%d, GPA=%.1f\n", student.id, student.gpa); print_student_by_value(student); printf("按值传递后: ID=%d (未改变)\n", student.id); print_student_by_pointer(&student); update_student_gpa(&student, 3.8f); printf("更新GPA后: GPA=%.1f\n", student.gpa); }
int main(void) { demonstrate_struct_basics(); demonstrate_struct_parameters(); return 0; }
|