In: Computer Science
C++ Code
Write a program that performs the following:
a) Declare long variables value1 and value2. The variable value1
has been initialized to 200000. Declare the variable longPtr to be
a pointer to an object of type long.
b) Assign the address of variable value1 to pointer variable
longPtr.
c) Display the value of the object pointed to by longPtr.
d) Assign the value of the object pointed to by longPtr to variable
value2.
e) Display the value of value2.
f) Display the address of value1.
g) Display the address stored in longPtr.
/* * C++ prgram to illustrate Pointers in C++ */ #include <iostream> using namespace std; int main() { // (a) long value1 = 200000, value2; long *longPtr; // (b) longPtr = &value1; // (c) cout << "Value of object pointed by longPtr: " << *longPtr << endl; // (d) value2 = *longPtr; // (e) cout << "Value of value2: " << value2 << endl; // (f) cout << "Address of value1: " << &value1 << endl; // (g) cout << "Address stored in longPtr: " << longPtr << endl; return 0; }
Note: For queries, drop me a comment.