6.7 Hierarchical or Nested Structures


Objectives:

At the end of this lesson you will be able to:

Use nested Structures

Structures can also be nested so that a valid element of a structure can also be in its turn with another structure.

Nesting of structures is placing structures within structure. You can nest a structure within another structure, and therefore keep associated elements together. The declaration syntax is what you would expect, as you can see in the following structure, which implements a push-down stack as a very simple linked list so it “never” runs out of memory.

Members in different structure can have the same name, since they are at different position.

Nested structures encapsulate structure definitions and make them an integral part of an enclosing structure definition. Use the . or -> operators in succession to access the member you want from a nested structure.


6.7 Hierarchical or Nested Structures



After the previous declaration we could use any of the following expressions:


(where, by the way, the last two expressions refer to the same member).