How to use a union along with two structs + more - c

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;

Related

making an array with a specific size that includes elements in data structure datatype

I want to make an array with 100 elements that include structure data type in C
#include <stdio.h>
struct student{
char name[20];
float grade[15];
struct date{
int day;
int month;
int year;
};
};
int main(){
struct std[100];
}
With a structure type like this:
struct student{
char name[20];
float grade[15];
struct date_tag{
int day;
int month;
int year;
} date;
};
You can do things like this
int main(){
struct student std[100];
std[99].grade[14]=1.5;
std[99].grade[14].date.day=28;
}
The identifier date_tag in that code is called a struct tag.
The corresponding type is struct date_tag and it is used to define a member of of that type within the the type struct student. The member is called date.
Within main() an array of struct students is defined and then accessed for demonstration purposes.

Build a struct with a struct inside

typedef struct Course {
char *CourseID;
char *CourseName;
struct Course* next;
} COURSE;
typedef struct Student{
int ID;
struct Student* next
}STUDENT;
I want to build a list of courses and then a list of students with the following
each student has an ID and some courses from the courses list and a grade of each course.
but how can I make that declaration inside the STUDENT struct? I cant understand that
For example
Student: 3049583222
Course: Biology (from course list)
grade: 30
and so on, every student could have all courses from the list.
My idea:
Linked List of Students.
Every student has a linked list of its courses with the grade.
The linked list elements have a pointer to the detailed course description.
// Description of a course
typedef struct Course {
char *CourseID;
char *CourseName;
} COURSE;
// Struct to hold the students grade, as a linked list to generate a list of all courses the student has.
typedef struct coursesTaken {
int grade;
COURSE* courseDescription;
struct coursesTaken* nextCourse;
} COURSES_TAKEN;
// The student with ID and pointer to his first course.
typedef struct Student{
int ID;
COURSES_TAKEN* firstCourse;
struct Student* next;
}STUDENT;
There can be multiple ways to do that, below shown are two approaches.
You can pick one and build on it.
typedef struct Course {
char *CourseID;
char *CourseName;
struct Course* next;
} COURSE;
typedef struct Student{
int ID;
}STUDENT;
//approach 1
#define NIDS 10
#define NCOURSES 20
typedef struct Student_Course_1{
STUDENT STU_ID[NIDS]; /* to store list of student ids */
COURSE STU_Courses[NCOURSES]; /* to store list of courses */
}COURSES_OF_STUDENT_1;
//approach 2
typedef struct Student_Course_2{
STUDENT STU_ID;
COURSE STU_Courses;
}COURSES_OF_STUDENT_2;
int main()
{
COURSES_OF_STUDENT_1 cs1; /* this has list of students and courses inside it */
COURSES_OF_STUDENT_2 cs2[NIDS]; /* you can use NIDS list of students and courses info with this */
return 0;
}

Structure within structure and function in C

I've been doing the task of reading data and creating particular structures.
In one structure (which contains in itself another structure) eclipse shows "field 'birth' has incomplete type".
I've searched through web, but it looks like there is some specific mistake.
(Here is shortened version of the code)
typedef struct{
int birthday_day;
int birthday_month;
int birthday_year;
} birthday;
typedef struct{
int id;
char name[20];
struct birthday birth;
}user;
user usser[100];
int i;
for (i=0;i<100;i++){
fscanf(input, "%s %i %i %i %i", usser[i].id,
usser[i].name, usser[i].birth.birthday_day, usser[i].birth.birhday_month,
usser[i].birth.birthday_year
};
typedef struct _birthday{
int birthday_day;
int birthday_month;
int birthday_year;
} birthday;
typedef struct{
int id;
char name[20];
struct _birthday birth;
}user;
or
typedef struct{
int id;
char name[20];
birthday birth;
}user;
in your example "birthday" is a new type which doesn't need the keyword "struct". That's why you get the error. You can use this type or give a name to the struct and use it with the keyword struct.

Can't print structure variable

#include <stdio.h>
#include <stdlib.h>
typedef struct {
char name[20];
int age;
} employee;
int main(int argc, char** argv)
{
struct employee em1 = {"Jack", 19};
printf("%s", em1.name);
return 0;
}
This doesn't seem to work because, as the compiler says, the variable has incomplete type of 'struct employee'. What's wrong?
Remove struct from
struct employee em1 = {"Jack", 19};
You used
typedef struct
{
char name[20];
int age;
}
with the purpose of not requiring to type struct anymore.
The problem is that you made the struct a typedef, but are still qualifying it with struct.
This will work:
employee em1 = {"Jack", 19};
Or remove the typedef.
To use struct employee em1 = ... you need to declare the struct with a tag.
struct employee /* this is the struct tag */
{
char name[20];
int age;
} em1, em2; /* declare instances */
struct employee em3;
typedef creates a type alias which you use without the struct keyword.
typedef struct employee employee;
employee em4;
As you have already typedef your structure, you are not required to add struct keyword again.
typedef struct Employee{
char name[20];
int age;
} employee;
int main(int argc, char** argv)
{
employee em1 = {"Jack", 19};
printf("%s", em1.name);
return 0;
}

Linked List Help, Singly Linked (Multiple Structures) (C Programming)

I need some help with Linked Lists.
I have figured out how to do individual linked list, but I am struggling when trying to implement multiple struct's and lists.
My last program was all used with Structs but now I must implement linked list's.
It says to use "External Pointers" in the functions to use in traversing through the various lists.
This is homework for one of my classes, I am not asking for you all to do it for me, but I am asking to help point me in the right direction.
The structs are as follows:
struct stockItem
{
char stockName[60];
char stockType[60];
int itemNumber;
float actualCost;
float markUp;
int totalCurrentInventory;
int monthlyRestock;
float price; //stores actual cost + markup
};
struct roomData
{
float widthFeet, widthInch;
float lengthFeet, lengthInch;
char roomName[100];
int roomNumberOfType;
char roomType[6]; //char of room type
int roomStock[100][2]; //for storing each room stock types
int roomHasStock; //if the room has a stock avaliable
int roomStockCount; //how many stocks the room has
float area; // sq ft
float rentalRate;
float profitsPerRoom;
float netProfit;
float grossProfit;
char stockLine[200];
};
struct staffData
{
char firstName[100];
char lastName[100];
char fullName[100];
int employeeNumber;
char typeOfEmployee[10];
char payType[10];
float hourlyWage;
float salary;
int hours;
char address[150];
char city[150];
char state[10];
int zip;
char phone[30];
float yearlyTotalPay;
struct hireDate //holds staff hire date
{
int month;
int day;
int year;
}hireDate;
struct birthDate //holds staff birth date
{
int month;
int day;
int year;
}birthDate;
};
typedef struct YourStructNode_ {
struct YourStructNode_ * next;
struct YourStructNode_ * prev;
YourStruct data;
} T_YourStructList;
Replace "YourStruct" by the name of your structs to make a doubly linked list.
Even if you make more than once T_XXXX_List with this pattern you should manipulate the list with the same function since the two first fields of T_Node is always the same.
Write add, insert, remove functions to manipulate this structure.
Is your linked list supposed to utilize the structs that you have developed? This way you have a linked list where each node contains an instance of all of those structs you listed.
struct node {
struct node *left;
struct node *right;
roomData room;
stockItem stock;
staffData staff;
hireDate hire;
birthDate birth;
};

Resources