For each of the following, write a single statement that performs the specified task. Assume
that double precision variables value1 and value2 have been declared and value1 has been initialized to 0.7254.
a) Declare the variable dPtr to be a pointer to an object of type double.
b) Assign the address of variable value1 to pointer variable dPtr.
c) Print the value of the object pointed to by dPtr.
d) Assign the value of the object pointed to by dPtr to variable value2.
e) Print the value of value2.
f) Print the address of value1.
g) Print the address stored in dPtr. Is the value printed the same as value1’s address?
#include <iostream.h>
using namespace std;
int main()
{
double value1;
double value2=20.4568;
(a) double* dPtr;
(b) dPtr =&value1;
(c) cout<<*dPtr<<"\n";
(d) value2=*dPtr;
(e) cout<<value2<<"\n";
(f) cout<<&value1<<"\n"; //OR
cout<<dPtr<<"\n"; //BOTH ARE SAME
(g) cout<<dPtr<<"\n";
cout<<&value1;
// ANSWER:- YES, BOTH ARE SAME
return 0;
}
Comments
Leave a comment