QUESTION 07
What is a nested structure?
2 marksAnswer
A structure containing another structure as a member is nested. A member such as s.date.day is reached through successive member selections.
QUESTION 07
A structure containing another structure as a member is nested. A member such as s.date.day is reached through successive member selections.
QUESTION 08
Complex records combine simpler members, arrays, nested structures or pointers to represent more detailed information.
struct Date { int day, month, year; };
struct Student { int roll; struct Date joined; char name[40]; };s.joined.day selects a day inside the nested date. s.name is an array within the student object and can hold at most 39 non-null text bytes plus its terminator. The nested object and the array are parts of each record, rather than separately allocated targets.
A pointer member can identify separate storage, but it does not automatically allocate or own that storage. If a structure is copied, the pointer value is copied. Its target is not duplicated automatically. An ownership rule must say which operation creates, shares or frees that target.
If two records are copied and both contain the same pointer address, both can reach the same target. Freeing the target through one record leaves the other pointer unusable for access. A deep copy is a deliberate process that allocates and copies separate target objects; ordinary structure assignment does not do this.
Choose nested objects for values that belong directly to a record and pointers for relationships or separately managed storage. Direct member selection is O(1), while processing a variable-length member or duplicating its target depends on that data’s size. Write clear lifetime and capacity rules.