Difficulty: Medium
Correct Answer: Error due to assigning a const int address to a non const pointer
Explanation:
Introduction / Context:
This question examines const correctness in C and what happens when you try to circumvent const by using a non const pointer. It focuses on the type system rather than the exact runtime behavior, highlighting that assigning the address of a const object to a non const pointer is a constraint violation that the compiler must diagnose.
Given Data / Assumptions:
• x is declared as const int x = 5; which means its value must not be modified through a const correct interface.
• ptrx is declared as int *ptrx; a pointer to non const int.
• The assignment ptrx = &x; attempts to store the address of x in a non const pointer.
• The code then writes *ptrx = 10; and prints x.
• We assume a standards compliant C compiler that enforces const correctness.
Concept / Approach:
In C, the type of &x is const int *. Assigning a const int * to an int * without an explicit cast is not allowed; it discards qualifiers and violates type safety. The C standard requires that such an assignment be diagnosed as a constraint violation. Therefore, the program is ill formed and should produce at least a warning or an error, typically preventing successful compilation if treated strictly. Although some compilers may still generate code and lead to undefined behavior at runtime, the correct conceptual answer in an exam context is that this is an error.
Step-by-Step Solution:
Step 1: Determine the type of &x: because x is const int, &x has type const int *.
Step 2: Determine the type of ptrx: it is int *, a pointer that can legally modify the int it points to.
Step 3: The assignment ptrx = &x; attempts to convert const int * to int *, discarding the const qualifier.
Step 4: This kind of assignment is forbidden in standard C; the compiler must issue a diagnostic.
Step 5: Because of this type mismatch, the program should be considered as having an error and not relied upon to compile or run correctly.
Verification / Alternative check:
If you correct the code to use const int *ptrx = &x; then *ptrx = 10; would also be rejected because it tries to write through a pointer to const. This demonstrates that the language intends x to be read only after being declared const. Any attempt to bypass this by discarding const qualifiers is treated as undefined or erroneous. Compilers that enforce warnings as errors will refuse to compile the original code, confirming that "Error" is the safest and most standard conforming interpretation.
Why Other Options Are Wrong:
Option A ("5") assumes that the attempt to modify x is ignored and the original value is always printed, which is not guaranteed; the behavior is undefined if the code is forced to run. Option B ("10") assumes that the modification always succeeds, which again is not guaranteed and depends on implementation details. Option D ("A random garbage value") suggests runtime undefined behavior, but the key exam point is that the code is already in error at the type system level; the primary correct answer focuses on the compile time issue.
Common Pitfalls:
Many learners think of const as merely an advisory keyword that can be bypassed without consequences. In reality, const is part of the type, and discarding it in pointer conversions violates constraints. Another pitfall is focusing only on the printf output instead of noticing the const mismatch earlier in the code. When analyzing code, always check pointer types and qualifier correctness before reasoning about runtime results, as type errors can invalidate any assumptions about output.
Final Answer:
Because the code assigns the address of a const int to a non const pointer and then writes through it, the correct conceptual answer is that it results in an error due to const mismatch.
Discussion & Comments