Difficulty: Hard
Correct Answer: DRAVID
Explanation:
Introduction / Context:
This question illustrates the difference between copying a structure that contains a pointer and copying the data being pointed to. It also highlights the potential dangers of modifying string literals in C. When a struct with a char * field is assigned to another struct, the pointer value is copied, not the underlying string. As a result, two structures may share the same string in memory, and modifying the string through one pointer affects what is seen through the other.
Given Data / Assumptions:
Concept / Approach:
When we write struct emp e2 = e1;, C performs a shallow copy of the structure. This means that the pointer n in e2 becomes equal to the pointer n in e1; both point to the same memory location where "Dravid" is stored. Calling strupr(e2.n) passes that pointer to the function, which then modifies each character of the string to its uppercase equivalent. Because e1.n and e2.n refer to exactly the same characters, printing e1.n afterwards shows the modified, uppercase version of the string.
Step-by-Step Solution:
Step 1: Initialize e1.n with the address of the string literal "Dravid".Step 2: Assign e2 = e1;, which sets e2.n to the same address as e1.n; the age field is also copied.Step 3: Call strupr(e2.n). The function reads each character from the shared string and converts it to uppercase.Step 4: After this call, the contents of the memory region originally holding "Dravid" are transformed to "DRAVID".Step 5: printf("\%s", e1.n); prints the string pointed to by e1.n, which is now "DRAVID".
Verification / Alternative check:
If instead of a pointer we had used an embedded character array in the struct, for example char n[20];, then struct assignment would copy the characters themselves, and e1.n and e2.n would refer to independent buffers. In that case, modifying e2.n would not change e1.n. The fact that the code uses char *n demonstrates that only the pointer is copied, not the underlying string contents, explaining why e1.n reflects the change.
Why Other Options Are Wrong:
Option B ("Dravid") would be correct only if the string had been duplicated or if e1.n pointed to a different memory location from e2.n. Option C claiming that it always crashes is not necessarily true in permissive environments, although modifying string literals is undefined behavior and may crash on some systems. Option D is wrong because neither pointer is set to NULL in the code; they both continue to point to the shared string.
Common Pitfalls:
A major pitfall is assuming that assigning structures or pointers automatically performs deep copies of the data they refer to. In C, struct assignment is a shallow copy that copies pointer values as-is. Another pitfall is modifying string literals; the C standard states that attempting to change them is undefined behavior, so the safe approach is to copy such strings into writable arrays first.
Final Answer:
Under the stated assumptions, the program prints DRAVID.
Discussion & Comments