使用更加简单的c语言程序编写上面程序
时间: 2024-10-20 08:17:07 浏览: 9
以下是用更简洁的C语言编写的类似程序,减少了不必要的细节:
```c
#include <stdio.h>
#define MAX_NAME_LENGTH 50
#define MAX_ADDR_LENGTH 100
typedef struct {
int id;
char name[MAX_NAME_LENGTH];
int age;
char addr[MAX_ADDR_LENGTH];
} Student;
// 设置学生信息
void set_student(Student *student, int id, const char *name, int age, const char *addr) {
student->id = id;
strncpy(student->name, name, MAX_NAME_LENGTH - 1); // 防止缓冲区溢出,用strncpy
student->age = age;
strncpy(student->addr, addr, MAX_ADDR_LENGTH - 1);
student->addr[MAX_ADDR_LENGTH - 1] = '\0'; // 添加终止符
}
// 打印学生信息
void print_student(const Student *student) {
printf("ID: %d\n", student->id);
printf("姓名: %s\n", student->name);
printf("年龄: %d\n", student->age);
printf("地址: %s\n", student->addr);
}
int main() {
// 创建并初始化学生
char student_name[] = "张三";
char student_addr[] = "北京市海淀区";
Student student1 = {1001, student_name, 18, student_addr};
// 打印学生信息
print_student(&student1);
return 0;
}
阅读全文