← Back to Pointers
Question 479
Deep Copy a Heap Hero Record
Deep Copy a Heap Hero Record
The struct is given to you:
typedef struct {
char *name;
int level;
} Hero;
Read one word (the name, at most 63 characters) and an integer level from stdin. Then:
1. Build the original on the heap: a Hero * from malloc whose name is its own heap copy of the word you read.
2. Deep copy it into a second Hero * - its own struct allocation and its own name allocation holding the same text.
3. Change the ORIGINAL only: set the first character of its name to 'Z', and add 1 to its level.
4. Print three lines:
"original: " followed by the original's name and level
"copy: " followed by the copy's name and level
"shared: " followed by 1 if the two name pointers are equal, or 0 if they are not
Never print a pointer itself - print the comparison.
Sample input:
Ada 7
Free the memory you allocate before the program ends — this question checks for memory leaks.Expected Output:
original: Zda 8 copy: Ada 7 shared: 0
= newline. The output ends with a newline — print one after the last line.
Hints4 available
Sign in to use hintsTopics:
Pointers
1
Tab to indent (Esc then Tab to move on) · Ctrl+Enter to run · Shift+Alt+F to format · Ctrl+Space to expand shortcuts (p, fori)
Your Output
Run your code to see the output here...
Test Cases
Run your code to see test case results.