Tin học cơ sở 3 pot

26 1.6K 0
Tin học cơ sở 3 pot

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Tin h c c s 3ọ ơ ở Structure Outline • Structured Data types • Structures as function arguments • Self-referential types: linked list Lê Nguyên Khôi 2 Structure • 2 nd aggregate data type: struct • Recall: aggregate meaning "grouping" – Recall array: collection of values of same type – Structure: collection of values of different types • Treated as a single item, like arrays • Major difference: Must first "define" struct – Prior to declaring any variables Lê Nguyên Khôi 3 Structure • Many structured collections of data are: – Heterogeneous (components are different types) – Fixed-size (well-defined set of components) • For example: Student Record Name Nguyen Van A Student Id 1234567 Course INT1005 Date of Birth 01/01/1993 Gender Male Lê Nguyên Khôi 4 Structured Data Types • A structure is a collection of variables, perhaps of different types, grouped together under a single name. • Structures: – Help to organize complicated data into manageable entities. – Expose the connection between data within an entity – Are defined using the struct keyword. – Sometimes referred as records. Lê Nguyên Khôi 5 Structured Data Types • Define struct globally (typically) • No memory is allocated – Just a “placeholder” for what struct will “look like” • To define struct, we must give: – Name of struct – Name of each field – Type of each field (could be another record/struct) Lê Nguyên Khôi 6 Defining Struct • Defining a new data type using struct struct date  name of new "type" { int day;  member names int month; int year; };  “;” after “}” struct date yesterday; • Once defined, this new data type could be used as other data types: – Variable of this type could be assigned to another variable of the same type – Variable of this type could be passed as a parameter to a function Lê Nguyên Khôi 7 Declare Structure Variable • With structure type defined, now declare variables of this new type: struct date today; – Just like declaring simple types – Variable today now of type struct date – It contains "member values" • Each of the struct "parts" Lê Nguyên Khôi 8 Accessing Structure Members • Dot Operator to access members – today.day – today.month – today.year • Called "member variables" – The "parts" of the structure variable – Different structs can have same name member variables • No conflicts Lê Nguyên Khôi 9 Defining New Type Struct • Could use typedef to define a new type struct typedef struct date Date; • And then we could use Date as other basic type without the need of including struct Date today; • Compare to previous declaration of yesterday struct date yesterday; Lê Nguyên Khôi 10 [...]... keyword typedef to make our own definitions: typedef float Floating; • This means variables can be declared as Floating but they will actually be of type float • If we later decide we need more precision, we can change to: typedef double Floating; without changing the codes Lê Nguyên Khôi 19 Combining struct and typedef struct employee { char name [30 ]; int id; char position[20]; float salary; }; typedef... = {31 , 12, 20 13} ; – Declaration provides initial data to all three member variables Lê Nguyên Khôi 12 Structure Assignments • Given structure named Fruit • Declare two structure variables: Fruit apples, oranges; – Both are variables of type "Fruit" – Simple assignments are legal: apples = oranges; • Simply copies each member variable from apples into member variables from oranges Lê Nguyên Khôi 13 Structure... a legal expression) – compare based on ordering (, ) (i.e apples < oranges is not a legal expression) – arithmetic operations (i.e apples + oranges is not a legal expression) – reading from or writing to text files (i.e no printf(apples) no scanf(&oranges)) • If you need such operations, write your own functions Lê Nguyên Khôi 14 Structure and Pointer • As for other types: – (struct x)* is a pointer... type • Each element in the linked list need: – some information (the data) – a connection to the next item • The individual elements in data structures like this are often called nodes Lê Nguyên Khôi 23 Linked Lists • A node could be represented by this struct list_node { int data; struct list_node *next; }; • How can we represent the whole list? • We need a pointer to the first node: struct list_node . Tin h c c s 3 ơ ở Structure Outline • Structured Data types • Structures as function. example: Student Record Name Nguyen Van A Student Id 1 234 567 Course INT1005 Date of Birth 01/01/19 93 Gender Male Lê Nguyên Khôi 4 Structured Data Types • A

Ngày đăng: 08/03/2014, 00:20

Từ khóa liên quan

Mục lục

  • Tin học cơ sở 3

  • Outline

  • Structure

  • Slide 4

  • Structured Data Types

  • Slide 6

  • Defining Struct

  • Declare Structure Variable

  • Accessing Structure Members

  • Defining New Type Struct

  • Structure Example

  • Structure Initialization

  • Structure Assignments

  • Structure Other Operations

  • Structure and Pointer

  • Slide 16

  • Slide 17

  • Structure as Function Argument

  • Other Type Definition

  • Combining struct and typedef

Tài liệu cùng người dùng

Tài liệu liên quan