I'm having trouble with my assignment and was hoping to get some help.
I'm suppose to have two structs, volunteer and employee, and a union 'person' that takes firstname, lastname, telenumber + either volunteer or employee as a struct.
I have had experience using unions + structs before, but I'm suppose to have additional information in the union. I was wondering how I can set it up properly, this is what I have so far.
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;
Any help would be great. This is the task instruction I'm stuck on.
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.
I think you are overthinking this: you need a structure to represent person. The key part is
"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;
This should do what you ask:
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;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.