Question: Question 7 (25) (a) What is a pointer? (b) What is a dereferencing operator? (c) What is the difference between assignment statements p1 = p2;
Question 7 (25)
(a) What is a pointer?
(b) What is a dereferencing operator?
(c) What is the difference between assignment statements p1 = p2;
and
*p1 = *p2;
(d) What is a dangling pointer?
(e) What is a dynamic variable?
(f) What is the purpose of the new operator?
(g) What is the purpose of the delete operator?
(h) What is the freestore (also called the heap)?
(i) What is the difference between dynamic variables and automatic variables?
(j) What is a dynamic array?
(k) What is the advantage of using dynamic arrays?
(l) What is the relationship between pointers and arrays?
(m) Explain what is the difference between int* p1, p2;
and
typedef int* IntPtr;
IntPtr p1, p2;
(n) For each of the following, write a single C++ statement that performs the identified task. (7)
(i) Declare two variables fPtr1 and fPtr2 to be pointers to objects of type double.
(ii) Create a dynamic variable to which fPtr1fPtr1 points.
(iii) If the pointer fPtrfPtr22 is undefined (i.e. it does not point to any variable), let it point to the same variable that fPtr1fPtr1 points to.
(iv) Print the address of the object pointed to by fPtr1fPtr1.
(v) Print the value of the object pointed to by fPtr2.fPtr2.
(vi) Release the memory occupied by the dynamic variable to which fPtr1fPtr1 points.
(vii) Assign null values to the pointers fPtr1 and fPtr2.
(o) Use diagrams similar to displays 9.1 and 9.3 in Chapter 9 in Savitch to trace the following C++ code and show the output. (6)
int x = 10, y = 20, *p, *q;
p = &x;
q = &y;
*p = 20;
*q += 10;
cout << x << " " << y << endl;
cout << *q << " " << *p << endl;
*p = *q + 5;
cout << *p << " " << *q << endl;
q = p;
cout << *p << " "<< *q << endl;
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
