Skip to study material
← Question index

QUESTION 08

Explain nested structures and structures containing arrays or pointers.

5 marks

Answer in brief

Complex records combine simpler members, arrays, nested structures or pointers to represent more detailed information.

Nested example

struct Date { int day, month, year; };
struct Student { int roll; struct Date joined; char name[40]; };

Access

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.

Pointer members

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.

Shallow-copy example

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.

Design and complexity

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.