C++ references vs pointers: evaluate two claims Pointer to a reference and reference to a pointer are both valid. When we use a reference, we are referring to its referent (the underlying object).
-
AOnly 1 is correct.
-
BOnly 2 is correct.
-
CBoth 1 and 2 are correct.
-
DBoth 1 and 2 are incorrect.
Answer
Correct Answer: Only 2 is correct.
Explanation
Introduction / Context:This question checks nuanced type-formation rules and semantics for references. It asks whether you can form a pointer-to-reference type and whether a reference name denotes the underlying object directly in expressions. Understanding these points helps avoid invalid type declarations and improves API design.
Given Data / Assumptions:
- We use standard C++ type rules.
- References must bind to objects and are not objects themselves.
- A reference can bind to a pointer type (i.e., reference-to-pointer is allowed).
Concept / Approach:
A “pointer to a reference” is ill-formed, because references do not exist as independent objects to which you can point. However, a “reference to a pointer” is valid; for example, int* p; int*& rp = p; binds a reference to a pointer variable. Separately, when you use a reference in an expression, the language automatically treats it as the referred-to object. Hence, statement 2 is true, but statement 1 is false because half of its conjunction (“pointer to a reference”) is invalid.
Step-by-Step Solution:
Test claim 1: pointer to reference — invalid; reference to pointer — valid ⇒ overall claim is false. Test claim 2: using a reference denotes the referent directly ⇒ true. Therefore, only statement 2 is correct.Verification / Alternative check:
Compilation check: int& *pp; is rejected; int*& rp = p; is accepted. Using rp in expressions behaves exactly like using p, confirming “refer to the referent.”
Why Other Options Are Wrong:
Only 1 — wrong because pointer-to-reference is not a legal type.
Both correct — wrong because claim 1 is half-invalid.
Both incorrect — wrong because claim 2 is valid.
Common Pitfalls:
- Thinking references are first-class objects; they are aliases and cannot be the target of pointers.
- Confusing reference-to-pointer with pointer-to-reference.
Final Answer:
Only 2 is correct.