Skip to main content

Practice ยท 3 of 4

Borrowing Rules

A struct holds a BORROWED string. Implement the documented discipline: ``c typedef struct { const char *name; int score; } Record; /* in boilerplate */ /* initializes the record with a borrowed name (NOT copied, NOT freed by the record). Caller must keep name alive as long as the record lives. */ void record_init(Record *r, const char *name, int score); /* returns the borrowed name (or NULL) */ const char *record_name(const Record *r); /* prints "name=score\n" (e.g. "ada=99\n") using the borrowed name */ void record_print(const Record *r); ``

Difficulty: intermediate

Back to lesson: Practice: Ownership Patterns Gym