c语言结构体-解决c中没有类的问题

1.任务需求:

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

2.文件工程:

c语言结构体-解决c中没有类的问题_第1张图片

3.student.c


#include "student.h"

void showInfo(stu* a)
{
    printf("用户%s的信息为:\n",a->name);
    printf("\t%s\n",a->name);
    printf("\t%d\n",a->age);
    printf("\t%s\n",a->major);
}
bool init(stu *p,char *_name,int _age,char *_major)
{
    p->age=_age;
    memcpy(p->name,_name,strlen(_name));
    memcpy(p->major,_major,strlen(_major));
    return true;
}

4.student.h

#ifndef STUDENT_H_INCLUDED
#define STUDENT_H_INCLUDED
#include 
#include 
#include 
#include 
#define MAX_NAME 20
#define MAX_MAJOR 20
typedef struct
{
    char name[MAX_NAME];
    int age;
    char major[MAX_MAJOR];
} stu;
stu stus[6];
extern void showInfo(stu* a);
extern bool init(stu *p,char *_nam

你可能感兴趣的:(c语言,结构体)