[C.C++] c语言结构体-解决c中没有类的问题

338 0
Honkers 2025-5-23 08:25:18 来自手机 | 显示全部楼层 |阅读模式

1.任务需求:

需要模拟一个C++中的类,能对对象进行初始化操作,并显示用户信息。

2.文件工程:

3.student.c

  1. #include "student.h"
  2. void showInfo(stu* a)
  3. {
  4. printf("用户%s的信息为:\n",a->name);
  5. printf("\t%s\n",a->name);
  6. printf("\t%d\n",a->age);
  7. printf("\t%s\n",a->major);
  8. }
  9. bool init(stu *p,char *_name,int _age,char *_major)
  10. {
  11. p->age=_age;
  12. memcpy(p->name,_name,strlen(_name));
  13. memcpy(p->major,_major,strlen(_major));
  14. return true;
  15. }
复制代码

4.student.h

  1. #ifndef STUDENT_H_INCLUDED
  2. #define STUDENT_H_INCLUDED
  3. #include <stdbool.h>
  4. #include <string.h>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #define MAX_NAME 20
  8. #define MAX_MAJOR 20
  9. typedef struct
  10. {
  11. char name[MAX_NAME];
  12. int age;
复制代码

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Honkers

荣誉红客

关注
  • 4012
    主题
  • 36
    粉丝
  • 0
    关注
这家伙很懒,什么都没留下!

中国红客联盟公众号

联系站长QQ:5520533

admin@chnhonker.com
Copyright © 2001-2025 Discuz Team. Powered by Discuz! X3.5 ( 粤ICP备13060014号 )|天天打卡 本站已运行