{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

SFTW120-LectureNotes-11 - SFTW120 Programming Science...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
1 SFTW120 Programming Science Lecture Notes 11: C: Structure types (Textbook Chapter 11) © Robert P. Biuk-Aghai SFTW120 - Programming Science 2 Lecture Notes 11 Structure types square6 Data structures such as arrays are limited to storing data of the same type only square6 Often related data of different types need to be stored together, as a record (i.e. a collection of information about the same data object) square6 If a program wants to use records, it first needs to define the structure of the record: its components and their type square6 In C, a record structure is defined by creating a structure type
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
2 SFTW120 - Programming Science 3 Lecture Notes 11 Records Example: records of the planets in the solar system square6 Each record has information about a different planet square6 Each record has components of different types, e.g.: box2 Name: Jupiter (string) box2 Diameter: 142,800 km (real) box2 Moons: 4 (integer) box2 Orbit time: 11.9 years (real) box2 Rotation time: 9.925 hours (real) SFTW120 - Programming Science 4 Lecture Notes 11 Defining structure types square6 Structure types are defined with typedef struct , e.g.: typedef struct { char name[10]; double diameter; int moons; double orbit_time; double rotation_time; } planet_t; square6 Type planet_t can now be used to create variables for storing records with information about planets
Background image of page 2
3 SFTW120 - Programming Science 5 Lecture Notes 11 Declaring and initializing structures square6 A variable of the structure type can be declared and initialized: planet_t earth = {"Earth", 12713.5, 1, 1.0, 24.0); square6 Memory cells in earth : ? ? ? ? \0 h t r a E 12713.5 1 1.0 24.0 SFTW120 - Programming Science 6 Lecture Notes 11 Using structure types in type definitions square6 When defining a structure type, each of its components may be of a simple type, or may be a data structure such as a structure type; e.g.: typedef struct { double diameter; planet_t planets[9] ; char galaxy[20]; } solar_sys_t; This structure type contains a component that is another structure type: this is called a hierarchical structure
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}