Search for question
Question

AENG/ECE-505 Winter 2024 HW3-2 Let us assume the program has been successfully built, and you run the program step by step. #define MAX_STR_LEN 2 #define MAX_INT_LEN 5 2 1. Pointer 123 +56 1 ∞ int main(void) = "dead"; { char str[MAX_STR_LEN] char *pstr1, *pstr2; int arr[MAX_INT_LEN]; int *parr, *parr_start; int i; pstr1 pstr2 0 1 2 9 10 11 12 13 = 14 = str; &str[0]; 15 /* --- */ 16 17 = 'A'; 18 19 /* --- * 20 21 /* 22 23 for (i = 24 *(pstr1 + 2) pstr2++; *(pstr2 + 2) = 'D'; */ 0; i < MAX_INT_LEN; i++) { arr[i] = i+100; 25 } 26 /* --- 27 28 29 /* --- 30 31 i+200; 32 } 33 parr = parr_start = arr; */ for (i = 0; i < MAX_INT_LEN; i++) { *parr++ = parr = parr_start; 34 /* --- */ 35 36 return 0; 37 } AENG/ECE-505 Winter 2024 This is the memory block. Fill out the blank step by step as you go through the questions. If the block is not yet initialized at the specified line number, fill it with the '?' mark. Note that you must execute the program in CCS to identify the memory location. Memory Location Content (32-Bit Hex) Line 15 Line 19 Line 21 Line 26 Line 29 Line 34 0x200001E0 0x200001E4 0x200001E8 0x200001EC 0x200001FO 0x200001F4 0x200001F8 0x200001FC 0x20000200 1. When you reach Line 15, what values are filled? If its value is not initialized yet, use the '?' mark. Don't forget to fill the memory block as well Variable Туре Name Value Location arr parr str AENG/ECE-505 Winter 2024 pstr1 pstr2 2. Is pstr1 same or different with pstr2? Explain why it is so. 3. When you reach Line 19, what is the pstr2 value? 4. When you reach Line 21, what would be the value from the C code below? Explain why it is so. * (pstr1 + 3) 5. When you reach Line 26, what values are at the arr array? 6. When you reach Line 29, what would be the value from the C code below? Explain why it is so. *(parr + 2) 7. When you reach Line 34, what would be the value from the C code below? Explain why it is so. *(parr +1) 2. Call by Reference vs. Value - You would like to implement a C function that swaps two values. AENG/ECE-505 Winter 2024 1 2 3 4 5 6 void swap_call_by_value(int foo, int bar) { int temp = foo; foo = bar; 9 } bar = temp; void swap_call_by_reference (/* add your code here */) /* add your code here */ 10 { 11 12 13 14 15 16 } 17 18 19 { 20 21 22 23 /* */ 24 int main(void) int aaa = 10, bbb = 20; swap_call_by_value(aaa, bbb); swap_call_by_reference(&aaa, &bbb); 56900 222 25 /* */ 26 27 return 0; 28 } 1. Complete the swap_call_by_reference function. After the successful completion, let's assume that you run the program step-by-step using the debugger. 2. When you reach Line 23, what are the values of aaa and bbb? Explain why they are so. 3. When you reach 25, what are the values of aaa and bbb? Explain why they are so.