Difficulty: Easy
Correct Answer: None of above
Explanation:
Introduction / Context:
This problem checks understanding of “pointer to const” versus “const pointer.” Here, ptr is declared as char const *ptr
, which means the characters pointed to must not be modified through ptr. The pointer itself is not const and may be reassigned.
Given Data / Assumptions:
*ptr = 'a'
which attempts to modify via a const-qualified access.
Concept / Approach:
With char const *ptr
, the pointed-to data is read-only through ptr. Writing via *ptr
is a constraint violation. Reassigning ptr = yourbuf
is fine because ptr itself is not const. The precise error is “assignment of read-only location.” None of the provided choices (a, b, c) correctly name this, so “None of above” is the accurate selection.
Step-by-Step Solution:
Declare ptr as pointer to const.Attempt *ptr = 'a'; → compile-time error: expression must be modifiable lvalue / read-only location.Line ptr = yourbuf; → valid reassignment.
Verification / Alternative check:
Change the declaration to char *ptr = mybuf;
and the assignment will compile and run. Alternatively, change to char *const ptr = mybuf;
to make the pointer itself const and observe that ptr = yourbuf;
then fails.
Why Other Options Are Wrong:
(a) and (b) mention conversions not present. (c) claims no error, which is incorrect due to the attempted write through a const-qualified pointer.
Common Pitfalls:
Confusing char const *
(data const) with char *const
(pointer const); believing that writable arrays remove const restrictions imposed by a pointer type.
Final Answer:
None of above
Discussion & Comments