Solutions to Homework Assignment #6

1. Consider the following C program declarations of a company's employee records:

 typedef struct person {   struct {     char first[20];     char last[20];   } name;   char my_id[10];   struct {     int day;     int month;     int year;   } birthday;   char department[10];   char supervisor_id[10];   int salary; } employee; ¡@ employee data[20]; ¡@ employee get_employee(char id[10]);

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[12].

(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[12].birthday.year, data[12].birthday.month, data[12].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",

get_employee(data[12].supervisor_id).name.first,

get_employee(data[12].supervisor_id).name.last);

(d) Write a statement to print the department name where Peter works for.

printf("Peter works for %s Department.\n", data[12].department);

(e) Write a statement to print Peter's annual salary.

printf("Peter\'s annual salary is %d.\n", data[12].salary);

2. Consider the following C program declaration of a union type:

 union numbers {   char string[5][20];   int sequence[60];   struct {     double re;     double im;   } complex[20]; } num;

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[4]?