For each of the following, write a single statement that performs the single task. Define two integer
variables val1 and val2. Initialize the val1 to 2300.
1. Define a pointer myPointer to an object of type integer.
2. Assign the address of variable val1 to pointer.
3. Print the value of the object pointed to by myPointer.
4. Assign the value of the object pointed to by myPointer to variable val2.
5. Print the value of val2.
6. Print the address of val1.
7. Print the address stored in myPointer
#include <iostream>
using namespace std;
int main()
{
int val1, val2; //Declaring val1 and val2
val1=2300; //Initializing val1 to 2300
int *myPointer; // defining the pointer myPointer
myPointer=&val1; //Assigning the address of val1 to myPointer
cout<<"Value of object pointed to by the pointer is : "<<val1<<"\n"; //Printing value of object pointed to by myPointer
val2= val1; //Assigning value pointed to by myPointer to val2;
cout<<"Value of val2 is : "<<val2<<"\n"; // Printing the value of val2
cout<<"Adress of val1 is : "<<&val1<<"\n"; // Printing the address of val1
cout<<"Adress stored in myPointer is : "<<myPointer<<"\n"; //Printing the address stored in myPointer
return 0;
}
Comments
Leave a comment