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: 3855451 • 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

Given below is the type and value of each expression given below along with explanation.

Value of field at address (i_ptr + 1) = 0xFF03 + 1 = 0xFF04.

Int value at address 0xFF04 is 4 (i.e. 'd.day')

Character string starting at address 0xFF07 (i.e. "uly"). This is the fourth byte in 'Date' structure 'd' starting at 0xFF04.

Address of field year in third 'Date' structure within 'holiday' array

= 0xFF15 + 2*(size of Date) + offset of year

= 0xFF15 + 2*(0xD) + 0xC

= 0xFF3B

Prompt Expression (Type) Value Explanation Evaluate the expression. &x (Pointer) 0xFF01 Base address of variable 'x' Evaluate the expression. p (Pointer) 0xFF01 Value of pointer variable 'p' Evaluate the expression. *p (Coord) {3,4,5} Value of Coord 'x' as pointer variable 'p' holds base address of variable 'x' Evaluate the expression. p->x - p->y (int) -1 p->x - p->y = 3 - 4 = -1 Evaluate the expression. (*p).x (int) 3 Value of field 'x' within Coord 'x' (as deferenced pointer variable 'p' refers to Coord 'x') Evaluate the expression. i_ptr[1] (int) 4

Value of field at address (i_ptr + 1) = 0xFF03 + 1 = 0xFF04.

Int value at address 0xFF04 is 4 (i.e. 'd.day')

Evaluate the expression. d.month_name (char *) July Value of field 'month_name' in 'Date' structure variable 'd' Evaluate the expression. d.month_name[3] (char) y Character at index 3 (i.e. fourth character) in character string referred by 'd.month_name' What is printed? printf("%s", str); (char *) uly

Character string starting at address 0xFF07 (i.e. "uly"). This is the fourth byte in 'Date' structure 'd' starting at 0xFF04.

Evaluate the expression. d_ptr->day (int) 4 Value of field 'day' in 'Date' structure pointed by 'd_ptr'. 'd_ptr' points to address of 'Date' structure variable 'd' (i.e. 0xFF04). Evaluate the expression. d (Date) {4, 7, "July", 2017} Value of 'Date' structure 'd' Evaluate the expression. holiday[1].month_name[3] (char) u Character at index 3 (i.e. fourth character) in character string referred by 'holiday[1].month_name' Evaluate the expression. holiday->month (int) 7 Value of field 'month' in 'Date' structure pointed by 'holiday'. 'holiday' points to first 'Date' structure within 'holiday' array Evaluate the expression. &holiday[2].year (pointer) 0xFF3B

Address of field year in third 'Date' structure within 'holiday' array

= 0xFF15 + 2*(size of Date) + offset of year

= 0xFF15 + 2*(0xD) + 0xC

= 0xFF3B

What is printed? printf("%s", d.month_name); July Value of field 'month_name' in 'Date' structure variable 'd'