如何将联合与两个结构一起使用 + 更多
How to use a union along with two structs + more
我的作业遇到了问题,希望得到一些帮助。
我假设有两个结构,volunteer 和 employee,以及一个 union 'person',它将名字、姓氏、电话号码 + volunteer 或 employee 作为一个结构。
我以前有过使用联合+结构的经验,但我想在联合中有更多的信息。我想知道如何正确设置它,这就是我目前所拥有的。
typedef struct volunteer{
int hours;
int tasksCompleted;
}student;
typedef struct employee{
float salary;
int serviceYears;
int level;
}employee;
typedef union person{
char firstName[20];
char familyName[20];
char telephoneNum[10];
//employee e;
//volunteer;
}person;
任何帮助都会很棒。这是我坚持的任务说明。
Create a person record that consists of the common records: first name, family name and telephone and a union between the volunteer and employee record. Make sure that you add a field to discriminate between the two records types employee or volunteer.
我认为你想多了:你需要一个结构来代表人。关键部分是
"and a union between the volunteer and employee record."
typedef enum { employee_person, volunteer_person } person_type;
typedef struct person{
char firstName[20];
char familyName[20];
char telephoneNum[10];
person_type type;
union {
struct employee employee;
struct volunteer volunteer;
};
}person;
这应该可以满足您的要求:
typedef struct volunteer {
int hours;
int tasksCompleted;
} student;
typedef struct employee {
float salary;
int serviceYears;
int level;
} employee;
struct person {
char firstName[20];
char familyName[20];
char telephoneNum[10];
bool is_employee;
union {
employee e;
student s;
} info;
} person;
我的作业遇到了问题,希望得到一些帮助。
我假设有两个结构,volunteer 和 employee,以及一个 union 'person',它将名字、姓氏、电话号码 + volunteer 或 employee 作为一个结构。
我以前有过使用联合+结构的经验,但我想在联合中有更多的信息。我想知道如何正确设置它,这就是我目前所拥有的。
typedef struct volunteer{
int hours;
int tasksCompleted;
}student;
typedef struct employee{
float salary;
int serviceYears;
int level;
}employee;
typedef union person{
char firstName[20];
char familyName[20];
char telephoneNum[10];
//employee e;
//volunteer;
}person;
任何帮助都会很棒。这是我坚持的任务说明。
Create a person record that consists of the common records: first name, family name and telephone and a union between the volunteer and employee record. Make sure that you add a field to discriminate between the two records types employee or volunteer.
我认为你想多了:你需要一个结构来代表人。关键部分是
"and a union between the volunteer and employee record."
typedef enum { employee_person, volunteer_person } person_type;
typedef struct person{
char firstName[20];
char familyName[20];
char telephoneNum[10];
person_type type;
union {
struct employee employee;
struct volunteer volunteer;
};
}person;
这应该可以满足您的要求:
typedef struct volunteer {
int hours;
int tasksCompleted;
} student;
typedef struct employee {
float salary;
int serviceYears;
int level;
} employee;
struct person {
char firstName[20];
char familyName[20];
char telephoneNum[10];
bool is_employee;
union {
employee e;
student s;
} info;
} person;