Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. Evaluate expressions using structs. (30 points/2 point each). Use the followi

ID: 3855448 • Letter: 1

Question

1. Evaluate expressions using structs. (30 points/2 point each). Use the following macro definitions and variable declarations to evaluate the following expressions or statements. that an e evaluat e always has the value 1 Also, assume that char and int are the same size and both take up one location in LC-3 memory . . Also, assume the expressions are evaluated independently without updates. . In the case of arrays and structs, the initial values should be interpreted with the same meaning as C array initializers. . In the case of structs, assume that fields of a struct occupy adjacent locations in memory. The base address of a struct is the location of the first field. Consider these struct definitions struct coord int x; int y; int z: typedef struct coord Coord; struct date int day: int month; char month name [10] int year: typedef struct date Date:

Explanation / Answer

1. Evaluate expression. &x:

Answer: Observe the first table given in the question. It shows that Base address of 'Coord x' as '0xFF01'.

The expression '&x' means 'address of x'. Thus, the expression '&x' gives the address '0xFF01' when evaluated.

2. Evaluate expression. p:

Answer: variable 'p' is the pointer to the structure Coord. It stores the address of a Coord variable 'x'.

evaluating 'p' gives the address of Coord variable 'x': 0xFF01 (same as '&x').

3. Evaluate expression. *p:

Answer: '*p' of the pointer variable denotes 'the content of'. Here, the pointer variable 'p' contains the Coord variable 'x' in it. So, '*p' represents the Coord data type with value {3, 4, 5}.

4. Evaluate the expression. p->x - p_y: p->x = x.x = 3 and p->y = x.y = 4.

So, p->x - p->y = x.x - x.y = 3 - 4 = -1

5. Evaluate the expression. (*p).x: '*p' denotes 'content of'. Here, the content of 'p' is the Coord variable 'x'.

So, (*p).x = x.x = 3.

6. Evaluate the expression. i_ptr[1]:

Answer: The first table shows that i_ptr is an integer pointer with address '0xFF11'.

i_ptr = i_ptr[0]. So, content of i_ptr[1] = content of "address of 'i_ptr' + 1" = content at '0xFF12'.

On observing the table, we can conclude that '0xFF12' stores the pointer to Date variable '*d_ptr' in it.

7. Evaluate the expression. d.month_name:

Answer: 'd' is the Date variable with values {4, 7, "July", 2017}.

This means:

d.day = 4

d.month = 7

d.month_name = "July"

d.year = 2017

8. Evaluate the expression. d.month_name[3]:

Answer: d.month_name = "July".

d.month_name[0] = 'J'

d.month_name[1] = 'u'

d.month_name[2] = 'l'

d.month_name[3] = 'y'.

9. What is printed? printf("%s",str):

Answer: char *str contains the address '0xFF07'.

0xFF04 stores d.day = 4

0xFF05 stores d.month = 7

0xFF06 stores d.month_name[0] = 'J'

0xFF06 stores d.month_name[1] = 'u'

So, 'char *str' points to the value 'u' in the string 'July'. When printf("%s",str) is called, "uly" is printed in the screen.

10. Evaluate the expression. d_ptr->day:

Answer: From the first table, it is evident that the pointer to Date variable 'd_ptr' contains the address of the Date variable 'd' (0xFF04). Thus, 'd_ptr->day' is equivalent to 'd.day'. Here, d.day = 4.

11. Evaluate the expression. d:

Answer: 'd' contains the struct date variable with values {4, 7, "July", 2017} in it.

12. Evaluate the expression. holiday[1].month_name[3]:

holiday[0] = {4,7,"July",2018}

holiday[1] = {1,1,"January",2018}. From this, we can see that holiday[1].month_name = "January".

Thus, holiday[1].month_name[0] = 'J'

holiday[1].month_name[1] = 'a'

holiday[1].month_name[2] = 'n'

holiday[1].month_name[3] = 'u'.

13. Evaluate the expression. holiday->month:

Answer: 'holiday' is an array of struct date. When the variable is called without its index, like 'holiday', address of the first element is returned, i.e., 'holiday[0]'. Here, holiday[0] = {4, 7, "July", 2018}.

So, 'holiday->month' returns 'holiday[0].month', which is '7'.

14. Evaluate the expression. &holiday[2].year:

Answer: This expression returns the address of 'holiday[2].year'.

holiday[0] starts at '0xFF15'. It has three integers and four characters (July). (3+4 = 7)

So, holiday[1] starts at '0xFF15' + 7 = '0xFF1C'. holiday[1] contains three integers and seven characters (January). (3+7 = 10).

Similarly, holiday[2] start at '0xFF27' (address of holiday[2].day).

holiday[2].month = '0xFF28'. holiday[2].month_name contains 8 characters. So, holiday[2].year is at the address '0xFF28' + 9 = '0xFF32' = &holiday[2].year.

15. What is printed? printf("%s",d.month_name):

Answer: Date variable 'd' stores the value {4,7,"July",2017}.

So, "printf("%s",d.month_name)" prints "July" in the screen.