1/04/2010

DCT1063 Lab Solution (Lab 1, Section C no 8)

LAB 1:POINTER

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;icout<d. Write two separate statements that assign the starting address of array values to pointer variable vPtr.
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;icout<f. Use a for statement to print the elements of array values using pointer/offset notation with the array name as the pointer.
Your answer: for(i=0;icout<<*(values +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;icout<h. Refer to the fifth element of values using array subscript notation, pointer/offset notation with the array name as the pointer, pointer subscript notation and pointer/offset notation.
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:

Cara download Installer windows 10 dalam format ISO

1. Jika anda bercadang untuk download windows 10 melalui website rasmi windows - pilihan untuk download dalam format ISO tidak di berikan.  ...