using namespace std;
int x = 7;
int* ptr = &x;
cout << *ptr << " ";
cout << *ptr;
Answer: 7 8
- In this C++ code, an integer variable
xis declared and initialized with the value 7.
- A pointer
ptris declared and initialized to store the address of
xusing the address-of operator (
cout << *ptr << " ";prints the value pointed to by
ptr, which is the value of
x, i.e., 7. So, the first output is "7 ".
(*ptr)++;increments the value stored at the memory location pointed to by
ptr. In this case, it increases the value of
xfrom 7 to 8.
cout << *ptr;prints the updated value pointed to by
ptr, which is now 8. So, the final output is "8".
This code demonstrates how a pointer can be used to access and modify the value of a variable indirectly.