Answer to Question #229161 in C++ for salahuddin

Question #229161

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



1
Expert's answer
2021-08-24T16:58:57-0400
#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;
	
}

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