Turbo C (DOS): effect of structure assignment with a char* and using strupr on the copy.\n\n#include <stdio.h>\n#include <string.h>\n\nint main()\n{\n struct emp {\n char n;\n int age;\n };\n struct emp e1 = { "Dravid", 23 };\n struct emp e2 = e1;\n strupr(e2.n);\n printf("%s\n", e1.n);\n return 0;\n}\n

Difficulty: Medium

Correct Answer: DRAVID

Explanation:


Introduction / Context:
This question explores structure assignment semantics and pointer aliasing. It uses strupr on the copied structure’s char pointer under Turbo C (DOS), a historical environment where string literals were often placed in writable segments.


Given Data / Assumptions:

  • e1.n points to a character sequence initially "Dravid".
  • e2 is assigned from e1, so e2.n points to the same memory as e1.n.
  • strupr modifies the string in place under Turbo C assumptions.


Concept / Approach:
Structure assignment copies pointer values, not cloned strings. After assignment, e1.n and e2.n alias the same storage. Calling strupr(e2.n) converts the characters to uppercase in that single shared storage. Therefore printing e1.n shows the uppercased content.


Step-by-Step Solution:

After e2 = e1, both structures share the same char target.strupr(e2.n) converts the underlying characters to uppercase in place.printf of e1.n reflects the modified content: DRAVID.


Verification / Alternative check:
On systems where string literals are read-only, modifying them is undefined behavior and may crash. The question pins Turbo C under DOS, where this commonly appears to work and produces DRAVID.


Why Other Options Are Wrong:

  • Error: invalid structure assignment: Structure assignment is valid in C.
  • Dravid: Would be true only if e2.n pointed to a separate writable copy.
  • No output: Not applicable; there is a valid printf.


Common Pitfalls:
Assuming deep copy of pointed-to data during structure assignment; in C, only the pointer value is copied unless explicitly duplicating the string buffer.


Final Answer:
DRAVID

More Questions from Structures, Unions, Enums

Discussion & Comments

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