I hate to call upon you guys again, and I guess in a way I shouldn't be asking, but could you check over these answers on a question about pointers and see if they are right. If they are, then I am finally getting a hold of this whole pointer concept.
Q5 (10 pts): Suppose that the following declarations are in effect: Make sure you explain why on each part to get any credit.
(a) What is the value of *(p+3)?Code:int a [] = {5, 15, 34, 54, 14, 2, 52, 72}; int *p = &a[1]; int *q = &a[5];
(b) What is the value of *(q-3)?
(c) Can I perform addition and subtraction on pointers? If so, what is q – p?
(d) Is the condition p < q true or false?
(e) Is the condition *p < *q true or false?
(f) Can I do the following, *p = a [4]; ?
(g) What happens when I do this, *p = *q; ?
(h) What happens when I do this, p = q; ?
(i) Is it legal for me to write the following line: p = a; ?
(j) Is it legal for me to write the following line: p = a [1]; ?
Legal refers to whether the code will compile or not.
a) 14. *p=the second element in the array, 15. *(p+3) is the 5th element, 14.
b) 34. *q=the sixth element in the array, 2. *(q-3) is the 3rd element, 34.
c) You can only add pointers. There is no pointer subtraction.
d) True, because the addresses of elements in arrays fall in succession, so since the address of p is the first element in the array, and q is the 5th element, p<q.
e) False, because the contents found at p=15, and the contents found at q=2. Thus 15>2.
f) Yes, because when declaring pointers with arrays, the & is not needed. It is an exception to the pointer rule.
g) The contents found at *p now becomes = to the contents at *q because the second element in the statement copies its value to
the first.
h) The address of q is copied to to the address of p for the same reason as stated in g.
i) Yes, because an array is looked at as a constant, so no & is needed. Although, with removing the [], it automatically points to the first element in the array.
j) Yes, because the address of the second element of array a is copied to the points p.



LinkBack URL
About LinkBacks


