Concept
How can one structure be composed of another structure?
ComputerScienceOne / Declaration & Initialization
"typedef struct {\n\nint id;\n\nchar *firstName;\n\nchar *lastName;\n\ndouble gpa;\n\nDate dateOfBirth;\n\n} Student;\n\nCode Sample 23.1.: A Student structure declaration\n\ntypedef struct {\n\nint year;\n\nint month;\n\nint date;\n\n} Date;\n\nOnce we have defined a structure we can use it as we would a normal variable, so it makes sense that we could include it in another structure. This is a good illustration of a form of composition where one structure may be composed of other structures. Since the Student structure “owns” an instance of the Date structure, it is necessary to ensure that the Date structure is declared before the Student structure (just as we need to declare variables before we use them."
Related Ideas
- How can a date be modeled using a nested structure?ComputerScienceOne · Nested Structures
- How do you access members of a statically declared structure?ComputerScienceOne · Selection Operators
- How are structures defined and named in C?ComputerScienceOne · Alternative Declarations
- How should structure names and member variables be declared in C?ComputerScienceOne · Nested Structures
- What is the alternative way to declare a structure in C?ComputerScienceOne · Alternative Declarations
- How do you access members through a pointer to a structure?ComputerScienceOne · Selection Operators
- How does composition model objects made up of other objects?ComputerScienceOne · Composition
- How does composition allow objects to own instances of other objects?ComputerScienceOne · Composition