Difficulty: Medium
Correct Answer: By explicitly casting the literal to a far pointer type, for example scr = (char far *)0xB8000000L;
Explanation:
Introduction / Context:
In older DOS programming with Turbo C or similar compilers, programmers sometimes wrote directly to video memory at segment B800h for text mode output. This required using far pointers to specific segment:offset addresses. Assigning a numeric constant directly to a pointer without a cast can cause the compiler to issue a warning because of the type mismatch between integer literals and pointer types.
Given Data / Assumptions:
Concept / Approach:
In C, assigning an integer literal directly to a pointer usually produces a warning, because the compiler wants an explicit cast when converting between integers and pointers. The correct way to indicate that you are intentionally treating a numeric literal as a pointer is to cast the literal to the appropriate pointer type. In this case, scr should be assigned (char far *)0xB8000000L, making the conversion explicit and silencing the warning on compilers that support far pointers.
Step-by-Step Solution:
Step 1: Keep scr declared as char far *scr; so that it can point to the video memory region.Step 2: Change the assignment from scr = 0xB8000000; to scr = (char far *)0xB8000000L; adding both a type cast and an appropriate literal suffix.Step 3: The cast (char far *) tells the compiler to interpret the integer constant as a far pointer to char.Step 4: The L suffix ensures that the constant is treated as a long value, matching the size of a far pointer on that platform.Step 5: With this explicit cast, the compiler understands the programmer intent and suppresses the type mismatch warning.
Verification / Alternative check:
Turbo C documentation and example code often show assignments such as char far *scr = (char far *)0xB8000000L; for direct video memory access. These examples compile without warnings when using the correct cast. If you remove the cast, the compiler warns about converting from long to pointer without an explicit cast, which is exactly the situation described in the original code.
Why Other Options Are Wrong:
Option B suggests changing scr to int, but then *scr = 'A' would be invalid because scr would no longer be a pointer to char. Option C is incorrect because leaving scr uninitialized and relying on a default value is unsafe and does not assign the desired video memory address. Option D is wrong because the whole purpose of the cast is to indicate intentional conversion, which is a standard technique to eliminate such warnings.
Common Pitfalls:
Programmers sometimes ignore compiler warnings about pointer and integer conversions, which can hide real bugs. It is better practice to use explicit casts when intentionally converting between integers and pointers, especially in low-level code. Another pitfall is forgetting that these techniques are specific to legacy 16-bit environments and are not portable to modern protected-mode or 64-bit systems.
Final Answer:
You should explicitly cast the literal, for example scr = (char far *)0xB8000000L;, to eliminate the warning.
Discussion & Comments