Title: COMP 102 Programming Fundamentals I
1COMP 102Programming Fundamentals I
- Presented by Timture Choi
2Structure
- Structure User defined data types
- Collection of related data items
- Possibly of different types
- In C
- Called struct
- Can be composed of data of different types
- Array
- Can contain only data of the same type
3Structure
- E.g.
- Student record
- Student id, name, major, gender, start year,
- Bank account
- Account number, name, currency, balance,
- Address book
- Name, address, telephone number,
4Structure
- Individual components of a struct type
- Called members (or fields)
- Members can be of different types
- int, float, double, char, array, or even another
struct - A struct is named as a whole
- While individual members are named using field
identifiers - Complex data structures
- Can be formed by defining arrays of structs
5struct Basics
- Definition of a structure
- E.g.
struct ltstruct-typegt lttypegt
ltidentifier_listgt lttypegt ltidentifier_listgt
struct date int day int month int
year
struct studentRecord char gender int
id char dept5 char name20
6struct Basics
- Declaration of a variable of struct type
- Syntax
- ltstruct-typegt ltidentifier_listgt
- E.g.
- studentRecord student1, student2
- student1 and student2 are variables of
studentRecord type
7struct Basics
- The members of a struct type variable are
accessed with the dot (.) operator - Syntax
- ltstruct-variablegt.ltmember_namegt
- E.g.
- student1.gender 'M'
- student1.id 12345
- strcpy(student1.dept, "COMP")
- strcpy(student1.name, "Chan Tai Man")
8struct Basics
9struct-to-struct Assignment
- The values contained in one struct type variable
- Can be assigned to another variable
- Of the same struct type
- E.g. student1 student2
- E.g.
10Nested Structures
- Nest structures inside structures
- E.g.
struct point double x, ypoint
Pstruct line point p1, p2line
Lstruct triangle point p1, p2,
p3triangle T
11Arrays of Structures
- An array of structs
- Multiple types of data in each array element
- E.g.
studentRecord class100class98.gender
'M'class98.id 12345strcpy(class98.dept,
"COMP")strcpy(class98.name, "Chan Tai
Man")class0 class98
12Arrays inside structures
- We can use arrays inside structures
- E.g.
- Assign values to sq using the given square
struct square point vertex4 square
sq
13Initialize Data of struct Type
- Using assignment during declaration
- E.g.
// student record struct StudentRecord
double totalgrade char namename_size
char idid_size int gradeno_grades
StudentRecord student1 0.0, "CHAN Tai
Man", "123456", 80, 67, 34, 67
14Enumerated Type
- The enumerated type (enum)
- Derived from the integer type
- Each integer value is given an identifier
- Called an enumeration constant
- Syntax
- enum type_name enumeration_constants
- type_name variable_name
15Enumerated Type
enum days mon, tue, wed, thur, fri, sat,
sun days day_of_week // normal expression
day_of_week wed // ERROR miss match type
day_of_week 0 // CORRECT 0 is converted to
days day_of_week (days) 0 // Use
enumeration constant as an integer int x
tue // same as x 3 int day7
daytue 0 // same as day1 0
16enum and struct
enum suit club, diamond, heart, spade struct
card suit cardSuit int value
17SUMMARY
- By the end of this lab, you should be able to
- Declare and use of struct type
- Nested structure
- Array of struct
- Declare and use of enum type