← Back to Pointers
Question 477
Growable Score Roster with realloc and Owned Names
Growable Score Roster with realloc and Owned Names
The struct is given to you:
typedef struct {
char *name;
int score;
} Entry;
Read an integer n (always 1 or more), then n lines, each holding a one-word name (at most 63 characters) and an integer score.
Start with Entry *list = NULL; and capacity 0, and grow the array with realloc as entries arrive - do not allocate all n slots up front. Each entry's name must point at its own heap copy of the word you read, not at the buffer you read into.
Print every entry in input order as the name, a space, and the score. Then print a final line: "Top: " followed by the name and score of the highest scorer. If two entries tie for the highest score, print the one that appeared first.
Sample input:
3
Ada 91
Linus 78
Grace 95
Free the memory you allocate before the program ends — this question checks for memory leaks.Expected Output:
Ada 91 Linus 78 Grace 95 Top: Grace 95
= 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.