Solutions to Homework Assignment #6
1. Consider the following C program declarations of a company's employee records:
employee get_employee(char id);
where function get_employee() receives an employee's identification number and returns the employee record with the employee's identification number of the parameter. Answer the following question with explanation. Suppose Peter's record is data.
(a) What is the size of memory space for an employee? What is the size of array data?
The field name.first and name.last take 20 bytes each, my_id takes 10 bytes, birthday takes 12 bytes (four bytes for day, four bytes for month, and four bytes for year), department takes ten bytes, supervisor_id takes 10 bytes, and salary takes four bytes. However, since birthday.day is of the integer type, it must start at the beginning of a word. Hence, my_id actually takes 12 bytes. Totally, an employee record takes 88 bytes.
Array data has 20 employee records and it takes 88´20=1760 bytes.
(b) Write a statement to print Peter's birthoday in the format of "year/month/day".
printf("Peter\'s birthday is %4d/%2d/%2d\n",
data.birthday.year, data.birthday.month, data.birthday.day);
(c) Write a statement to print the name of Peter's supervisor, in the format of first name first and then the last name.
printf("Peter\'s supervisor is %s %s.\n",
(d) Write a statement to print the department name where Peter works for.
printf("Peter works for %s Department.\n", data.department);
(e) Write a statement to print Peter's annual salary.
printf("Peter\'s annual salary is %d.\n", data.salary);
2. Consider the following C program declaration of a union type:
Suppose the starting address of num is 0X403000. Answer the following question with explanation.
(a) What is the size of num?
Size of num is 320.
(b) What is the size of string?
Size of string: 100
(c) What is the size of sequence?
Size of sequence: 240
(d) What is the starting
Address of num.string: 0X403050
(e) What is the starting
address of num.complex.im?
Address of num.complex.im: 0X4030A8