SFTW120-LectureNotes-11

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 s Data structures such as arrays are limited to storing data of the same type only s 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) s If a program wants to use records, it first needs to define the structure of the record: its components and their type s 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 DocumentRight Arrow Icon
2 SFTW120 - Programming Science 3 Lecture Notes 11 Records Example: records of the planets in the solar system s Each record has information about a different planet s Each record has components of different types, e.g.: b Name: Jupiter (string) b Diameter: 142,800 km (real) b Moons: 4 (integer) b Orbit time: 11.9 years (real) b Rotation time: 9.925 hours (real) SFTW120 - Programming Science 4 Lecture Notes 11 Defining structure types s 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; s 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 s A variable of the structure type can be declared and initialized: planet_t earth = {"Earth", 12713.5, 1, 1.0, 24.0); s 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 s 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 DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 05/04/2011 for the course SFST 120 taught by Professor Dr.robert during the Spring '11 term at University of Macau.

Page1 / 10

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

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online