What will be printed by the following C++ code?
int x = 10;
int *p = &x;
int *q = &x;
if(p == q)
cout << "Same";
else
cout << "Different";
Options
-
A.
Same
-
B.
Different
-
C.
Compilation error
-
D.
More than one of the above
-
E.
None of the above
Correct Answer (Detailed Explanation is Below)
A.
Same
Detailed Explanation
Answer: (A) Same
Both p and q are initialized with the address of the same variable x.
Therefore, both pointers contain the same address.
The comparison p == q is true, so the program prints Same.
This demonstrates that pointers can be compared to determine whether they point to the same object or address.