Type qualification mismatch: binding a pointer to non-const int to the address of a const int\n\n#include<stdio.h>\n\nint main()\n{\n const int k = 7;\n int const q = &k; / constant pointer to non-const int */\n printf("%d", *q);\n return 0;\n}

Difficulty: Easy

Correct Answer: Error: cannot convert from 'const int *' to 'int const'

Explanation:


Introduction / Context:
This tests qualifier compatibility. The expression &k has type const int, but the code tries to initialize q as an int *const, i.e., a constant pointer to non-const int. That discards const on the pointed-to type.



Given Data / Assumptions:

  • k is const int.
  • &k is of type const int *.
  • q is declared as int *const (pointer itself const, target non-const).


Concept / Approach:
Type int *const means “a const pointer to int.” The target type is int (modifiable). Assigning the address of a const int to a pointer to non-const int would allow writes to a const object through q, which is forbidden. The correct type would be const int *const q = &k;.



Step-by-Step Solution:
Compute &k → const int *.Attempt to bind to int *const → discarding const on pointee.Compilation error: cannot convert from const int * to int *const.Fix: make q a pointer to const: const int *const q = &k;


Verification / Alternative check:
With the corrected declaration, dereferencing *q yields 7, but q cannot be used to modify k.



Why Other Options Are Wrong:
Rvalue/Lvalue diagnostics are not the issue. “No error” is incorrect because qualifiers are mismatched.



Common Pitfalls:
Confusing "const pointer" with "pointer to const"; overlooking that const applies to the pointee type here.



Final Answer:
Error: cannot convert from 'const int *' to 'int *const'

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion