Bifalgorithm post · Computer science one · Declaration & Initialization

How can one structure be composed of another structure?

How can one structure be composed of another structure? (Computer science one · Declaration & Initialization) — "typedef struct {\n\nint id;\n\nchar *firstName…

Ideas in this flowchart

  • Structures can be composed by including one structure in another.
  • The Student structure is declared with id, firstName, lastName, gpa, and Date dateOfBirth.
  • The dateOfBirth member has the Date structure type.
  • The Date structure is declared with year, month, and date.
  • A defined structure can be used like a normal variable.
  • Including Date in Student illustrates structure composition.
  • The Student structure owns an instance of the Date structure.
  • The Date structure must be declared before the Student structure.
  • This requirement is like declaring variables before using them.
Feed/

How can one structure be composed of another structure?

No cover

Channel

Computer science one

1 page

"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."

Flowchart preview
👍
0
0 comments0 shares

Sign in to view and post comments.