Hi, folks!
I just stuck with book exercise.
Here is exercise:
8.4
For each of the following, write a single statement that performs the specified task. Assume
that floating-point variables number1 and number2 have been declared and that number1 has been ini-
tialized to 7.3 .
a) Declare the variable fPtr to be a pointer to an object of type double and initialize the
pointer to nullptr .
b) Assign the address of variable number1 to pointer variable fPtr .
c) Display the value of the object pointed to by fPtr .
d) Assign the value of the object pointed to by fPtr to variable number2 .
e) Display the value of number2 .
f) Display the address of number1 .
g) Display the address stored in fPtr . Is the address displayed the same as that of number1 ?
double *fPtr = nullptr;
fPtr = &number1;
cout << "The value of *fPtr is " << *fPtr << endl;
number2 = *fPtr;
cout << "The value of number2 is " << number2 << endl;
cout << "The address of number1 is " << &number1 << endl;
Here is my code which catches error:
1 2 3 4 5 6 7 8 9 10 11 12 13
|
#include <iostream>
using namespace std;
int main()
{
double number1 = 7.3;
double number2 = 7.3;
double *fPtr = nullptr;
fPtr = &number1;
}
|
I've been told that there is no possibility to use these statements outside the function.
Book says it can.
Help me clarify that please.
Thanks in Advance