COMP 102 Programming Fundamentals I - PowerPoint PPT Presentation

1 / 17
About This Presentation
Title:

COMP 102 Programming Fundamentals I

Description:

Structure User defined data types. Collection of related data items ... enum suit {club, diamond, heart, spade}; struct card { suit cardSuit; int value; ... – PowerPoint PPT presentation

Number of Views:33
Avg rating:3.0/5.0
Slides: 18
Provided by: typey73
Category:

less

Transcript and Presenter's Notes

Title: COMP 102 Programming Fundamentals I


1
COMP 102Programming Fundamentals I
  • Presented by Timture Choi

2
Structure
  • 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

3
Structure
  • E.g.
  • Student record
  • Student id, name, major, gender, start year,
  • Bank account
  • Account number, name, currency, balance,
  • Address book
  • Name, address, telephone number,

4
Structure
  • 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

5
struct 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
6
struct 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

7
struct 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")

8
struct Basics
  • Overview

9
struct-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.

10
Nested Structures
  • Nest structures inside structures
  • E.g.

struct point double x, ypoint
Pstruct line point p1, p2line
Lstruct triangle point p1, p2,
p3triangle T
11
Arrays 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
12
Arrays inside structures
  • We can use arrays inside structures
  • E.g.
  • Assign values to sq using the given square

struct square point vertex4 square
sq
13
Initialize 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
14
Enumerated 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

15
Enumerated Type
  • E.g.

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
16
enum and struct
  • E.g.

enum suit club, diamond, heart, spade struct
card suit cardSuit int value
17
SUMMARY
  • 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
Write a Comment
User Comments (0)
About PowerShow.com