Section C. Coding Exercises
Question no 8.
Berikut adalah jawapan bagi soalan di atas. Aktiviti lab pada 6 jan 2010
8. For each of the following, write C++ statements that perform the specified task. Assume that unsigned integers are stored in two bytes and that the starting address of the array is at location 1002500 in memory.
a. Declare an array of type unsigned int called values with five elements, and initialize the elements to the even integers from 2 to 10. Assume that the symbolic constant SIZE has been defined as 5.
Your answer: unsigned int values[SIZE]={2,4,6,8,10);
b. Declare a pointer vPtr that points to an object of type unsigned int.
Your answer: unsigned int *vPtr;
c. Use a for statement to print the elements of array values using array subscript notation.
Your answer: for(i=0;i
Your answer: vPtr = values; or
vPtr=&values[0];
e. Use a for statement to print the elements of array values using pointer/offset notation.
Your answer: for(i=0;i
Your answer: for(i=0;i
g. Use a for statement to print the elements of array values by subscripting the pointer to the array.
Your answer: for(i=0;i
Your answer:
values[4] // array subscript notation
*(values + 4) // pointer/offset notation with the array name as the pointer
*(vPtr+4) //pointer subscript notation
vPtr[4] //pointer/offset notation
i. What address is referenced by vPtr + 3? What value is stored at that location?
Address: 1002506
Value: 8
j. Assuming that vPtr points to values[ 4 ], what address is referenced by vPtr -= 4? What value is stored at that location?
Address: 1002500
Value: 2
No comments:
Post a Comment