Answer to Question #350966 in C++ for BScInf

Question #350966

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 fPtr1 points.

(iii) If the pointer fPtr2 is undefined (i.e. it does not point to any variable), let it point to the same variable that fPtr1 points to.

(iv) Print the address of the object pointed to by fPtr1.

(v) Print the value of the object pointed to by fPtr2.

(vi) Release the memory occupied by the dynamic variable to which fPtr1 points.

(vii) Assign null values to the pointers fPtr1 and fPtr2


1
Expert's answer
2022-06-15T17:24:18-0400
#include <iostream>

using namespace std;

int main()
{
	//1)Declare two variables fPtr1 and fPtr2 to be pointers to objects of type double.
	double *fPtr1, *fPtr2;
	//2)Create a dynamic variable to which fPtr1 points.
	fPtr1 =new double(10);
	//3)If the pointer fPtr2 is undefined(i.e.it does not point to any variable), let it point to the same variable that fPtr1 points to.
	fPtr2 = fPtr1;
	//4)Print the address of the object pointed to by fPtr1.
	cout<< "fPtr1 address = "<<fPtr1<<endl;
	//5) Print the value of the object pointed to by fPtr2.
	cout << "fPtr2 value = " << *fPtr2 << endl;
	//6)Release the memory occupied by the dynamic variable to which fPtr1 points.
	delete fPtr1;
	//7)Assign null values to the pointers fPtr1 and fPtr2
	fPtr1 = NULL;
	fPtr2 = NULL;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS