CS 2505 OQ02: Pointers, Arrays, Memory Allocation, Number Representation Q A Reason ------------------------------------------------------------------------------------- For questions 1 through 4, the key is understanding the relationship between a pointer and its target. 1. 2 p1 is an integer pointer 2. 3 p1 has been dereferenced, thus *p1 indicates the target integer 3. 1 The & operator obtains an address for p1, giving type int** 4. 3 Remember the syntax gotcha when declaring multiple pointers, p2 has type int rather than int* ------------------------------------------------------------------------------- 5. 4 "CS2505 is so much fun" takes 21 characters, the "\n" gets the count to 22, and the implicit "\0" increases the count to 23 6. 5 The code will compile and run successfully 7. 2 myString[13] points to the 'm' in "much", so after line 3, partway points to the 's' in "so"; this prints the rest of the string, from that point, but the string includes a newline, and the format specifier provides a second one. 2 is the closest answer, but actually none of them are precisely correct. (We'll throw this one out when grading.) ------------------------------------------------------------------------------- 8. 1 Just use the conversion algorithm from the notes: 2 |12345 6172 1 3086 0 1543 0 771 1 385 1 192 1 96 0 48 0 24 0 12 0 6 0 3 0 1 1 0 1 9. 3 Group in nybbles and write it out: 110 0101 0010 1010 6 5 2 A 10. 3 From the notes on data representation. 11. 2 From the notes on data representation. 12. 3 Use the conversion algorithm above. 13. 1 Apply the conversion algorithm to 65, and then negate as 2's complement. 14. 1 Just add them. 15. 1 Same thing... ------------------------------------------------------------------------------- 16. 3 Try it for a short string. The index of the last char - the index of the first char is one too small. Basic counting principle here... 17. 2 We allocate an array with an extra cell for the terminator, and calloc() fills the bytes in the array with zeros. 18. 3,4 By design, the array cell after the last valid pointer is NULL. However, the test in 3 will also work correctly, so either answer is valid. 19. 2 Here, we deallocate the target of the current pointer in list[]. 20. 4 Now, we need to deallocate the array that held the pointers.