204216 -- C Programming · Structure definition in C: โครงสร้างของ...

26
204216 -- C Programming Chapter 12 Structures Adapted/Assembled for 204216 by Areerat T rongratsameethong A First Book of ANSI C, Fourth Edition

Transcript of 204216 -- C Programming · Structure definition in C: โครงสร้างของ...

Page 1: 204216 -- C Programming · Structure definition in C: โครงสร้างของ structure ... Common Programming Errors • Attempting to use structures and unions, as

204216 -- C Programming

Chapter 12Structures

Adapted/Assembled for 204216 by Areerat Trongratsameethong

A First Book of ANSI C, Fourth Edition

Page 2: 204216 -- C Programming · Structure definition in C: โครงสร้างของ structure ... Common Programming Errors • Attempting to use structures and unions, as

Objectives

• Single Structures• Arrays of Structures• Passing and Returning Structures• Unions (Optional)• Common Programming and Compiler Errors

A First Book of ANSI C, Fourth Edition 2

Page 3: 204216 -- C Programming · Structure definition in C: โครงสร้างของ structure ... Common Programming Errors • Attempting to use structures and unions, as

Introduction

• ขอมลแตละตวทแสดงในรปท 12.1 เรยกวา data field• ทก data field รวมกนเรยกวา หนง record

– ในภาษา C, เรยก record วา structure• รปแบบและขอมลของ structure แสดงดงรปท 12.2

A First Book of ANSI C, Fourth Edition 3

Page 4: 204216 -- C Programming · Structure definition in C: โครงสร้างของ structure ... Common Programming Errors • Attempting to use structures and unions, as

Single Structures• Structure definition in C: โครงสรางของ structure

ในภาษา C ดงแสดงดานลางstruct{

int month;int day;int year;

} birth;

– ขอมลทง 3 ตวทอยใน struct คอสมาชกของ struct– สาหรบการจองพนทใน memory จะจองพนทสาหรบขอมลแตละ

ตวตามขนาดของชนดขอมลสมาชกทประกาศใน struct

A First Book of ANSI C, Fourth Edition 4

ชอ struct

สมาชกของ struct

Page 5: 204216 -- C Programming · Structure definition in C: โครงสร้างของ structure ... Common Programming Errors • Attempting to use structures and unions, as

A First Book of ANSI C, Fourth Edition 5

Single Structures (2)

กาหนดคาขอมลใหสมาชกแตละตว

วธอางถงขอมลสมาชกแตละตว

Page 6: 204216 -- C Programming · Structure definition in C: โครงสร้างของ structure ... Common Programming Errors • Attempting to use structures and unions, as

Single Structures (3)• Multiple variables can be defined in one statement

struct {int month; int day; int year;} birth, current; birth และ current เปนตวแปรประเภท struct ทมโครงสราง

เหมอนกน คอ มสมาชก 3 ตว

• Common to list the form of the structure with no following variable names– The list of structure members must be preceded by a user-

selected structure type name – เราสามารถตงชอชนดของ struct ได เนองจาก ขอมลโดยปกตแลวมกจะม

โครงสรางขอมลหลากหลาย และหลายแบบ

struct Date{int month;int day;int year;

};A First Book of ANSI C, Fourth Edition 6

ชอชนดของ struct ควรตงชอขนตนดวยตวใหญ

Page 7: 204216 -- C Programming · Structure definition in C: โครงสร้างของ structure ... Common Programming Errors • Attempting to use structures and unions, as

A First Book of ANSI C, Fourth Edition 7

Single Structures (4)

ชอตวแปร struct ทมโครงสรางขอมลแบบ Date

ชอชนดของ struct

Page 8: 204216 -- C Programming · Structure definition in C: โครงสร้างของ structure ... Common Programming Errors • Attempting to use structures and unions, as

Single Structures (5)• Initialization of structures follows the same rules

as for the initialization of arrays:– struct Date birth = {12, 28, 1987};

• Structure members can be of any data typestruct PayRecord{char name[20];int idNum;double regRate;double otRate;

};

struct PayRecord employee = {"H. Price", 12387, 15.89, 25.50};

A First Book of ANSI C, Fourth Edition 8

ชอชนดของ struct ชอตวแปร

คาเรมตนของ name idNum regRate otRate

คาเรมตนของ month day year

Page 9: 204216 -- C Programming · Structure definition in C: โครงสร้างของ structure ... Common Programming Errors • Attempting to use structures and unions, as

Single Structures (6)

• Advantage of structures is when the same structure type is used in a list many times

• Individual members can be arrays and structures

struct{char name[20];struct Date birth;

} person;

– Example: person.name[4]

A First Book of ANSI C, Fourth Edition 9

Page 10: 204216 -- C Programming · Structure definition in C: โครงสร้างของ structure ... Common Programming Errors • Attempting to use structures and unions, as

Arrays of Structures

A First Book of ANSI C, Fourth Edition 10

Page 11: 204216 -- C Programming · Structure definition in C: โครงสร้างของ structure ... Common Programming Errors • Attempting to use structures and unions, as

A First Book of ANSI C, Fourth Edition 11

Inner braces are not necessary

Arrays of Structures (2)

Page 12: 204216 -- C Programming · Structure definition in C: โครงสร้างของ structure ... Common Programming Errors • Attempting to use structures and unions, as

Arrays of Structures (3)• Without explicit initializers, the numeric elements

of both static and external arrays or structures are initialized to 0 (or nulls)

• An inferior alternative to an array of structures is parallel arrays– Parallel arrays are two or more arrays, where each

array has the same number of elements and the elements in each array are directly related by their position in the arrays

– ไมควรประกาศ arrays หลายๆตวคขนานกนไป

เชน int id[11], char *name[11], float scores[11] ถาประกาศลกษณะน เวลาเรยกใชตองระวง คอตองเรยก index ทตรงกน

– They are rarely used any more:

A First Book of ANSI C, Fourth Edition 12

Page 13: 204216 -- C Programming · Structure definition in C: โครงสร้างของ structure ... Common Programming Errors • Attempting to use structures and unions, as

Passing and Returning Structures

• Individual structure members may be passed to a function in the same manner as any scalar variable (pass by value)– display(emp.idNum);//สามารถสงคาขอมลของ member บางตวได

– calcPay(emp.payRate,emp.hours); //สามารถสงคาขอมลของ member บางตวได

• On most compilers, complete copies of all members of a structure can also be passed to a function by including the name of the structure as an argument to the called function– calcNet(emp); // สามารถสงคาขอมลของทก member เขาไปได

A First Book of ANSI C, Fourth Edition 13

Page 14: 204216 -- C Programming · Structure definition in C: โครงสร้างของ structure ... Common Programming Errors • Attempting to use structures and unions, as

A First Book of ANSI C, Fourth Edition14

Pass by value

Passing and Returning Structures (2)

Page 15: 204216 -- C Programming · Structure definition in C: โครงสร้างของ structure ... Common Programming Errors • Attempting to use structures and unions, as

Passing and Returning Structures (3)• A structure can be passed by reference

– calcNet(&emp);– double calcNet(struct Employee *pt)– (*pt).idNum or pt->idNum

A First Book of ANSI C, Fourth Edition15

Page 16: 204216 -- C Programming · Structure definition in C: โครงสร้างของ structure ... Common Programming Errors • Attempting to use structures and unions, as

A First Book of ANSI C, Fourth Edition16

Passing and Returning Structures (4)

Page 17: 204216 -- C Programming · Structure definition in C: โครงสร้างของ structure ... Common Programming Errors • Attempting to use structures and unions, as

Passing and Returning Structures (5)

• ++ and -- can be applied to structures– ++pt->hours– (pt++)->hours– (++pt)->hours

A First Book of ANSI C, Fourth Edition17

Page 18: 204216 -- C Programming · Structure definition in C: โครงสร้างของ structure ... Common Programming Errors • Attempting to use structures and unions, as

Returning Structures

A First Book of ANSI C, Fourth Edition 18

Page 19: 204216 -- C Programming · Structure definition in C: โครงสร้างของ structure ... Common Programming Errors • Attempting to use structures and unions, as

Unions• A union is a data type that reserves the same

area in memory for two or more variablesunion{char key;int num;double price;

} val;

– Each of these types, but only one at a time, can actually be assigned to the union variable

– A union reserves sufficient memory locations to accommodate its largest member’s data type

A First Book of ANSI C, Fourth Edition 19

Page 20: 204216 -- C Programming · Structure definition in C: โครงสร้างของ structure ... Common Programming Errors • Attempting to use structures and unions, as

Unions (2)#include<stdio.h>struct Person {

char key;int num;double price;

};union Student {char key;int num;double price;

};struct Person1{

int age;char name[6];float height;

};union Student1{

int age;char name[6];float height;

}; int main() {

printf("sizeof(struct Person) = %d\n", sizeof(struct Person));printf("sizeof(union Student) = %d\n", sizeof(union Student));printf("sizeof(double) = %d\n", sizeof(double));printf("\nsizeof(struct Person1) = %d\n", sizeof(struct Person1));printf("sizeof(union Student1) = %d\n", sizeof(union Student1));printf("sizeof(float) = %d\n", sizeof(float));return 0;

} A First Book of ANSI C, Fourth Edition20

Outputsizeof(struct Person) = 16sizeof(union Student) = 8sizeof(double) = 8

sizeof(struct Person1) = 16sizeof(union Student1) = 8sizeof(float) = 4

Page 21: 204216 -- C Programming · Structure definition in C: โครงสร้างของ structure ... Common Programming Errors • Attempting to use structures and unions, as

Unions (3)

• A type name can be associated with a union to create templatesunion DateTime{long days;double time;

};union DateTime first, second, *pt;

• Pointers to unions use the same notation as pointers to structures

• Unions may be members of structures and arrays; structures, arrays, and pointers may be members of unions

A First Book of ANSI C, Fourth Edition 21

Page 22: 204216 -- C Programming · Structure definition in C: โครงสร้างของ structure ... Common Programming Errors • Attempting to use structures and unions, as

Unions (4)struct{char uType;union{char *text;double rate;

} uTax;} flag;

• rate is referenced as flag.uTax.rate• The first character of the string whose address

is stored in the pointer text is accessed as *flag.uTax.text

A First Book of ANSI C, Fourth Edition 22

Page 23: 204216 -- C Programming · Structure definition in C: โครงสร้างของ structure ... Common Programming Errors • Attempting to use structures and unions, as

Common Programming Errors

• Attempting to use structures and unions, as complete entities, in relational expressions

• Assigning an incorrect address to a pointer that is a member of a structure or union

• Storing one data type in a union and accessing it by the wrong variable name can result in an error that is particularly troublesome to locate

A First Book of ANSI C, Fourth Edition 23

Page 24: 204216 -- C Programming · Structure definition in C: โครงสร้างของ structure ... Common Programming Errors • Attempting to use structures and unions, as

Common Compiler Errors

A First Book of ANSI C, Fourth Edition 24

Page 25: 204216 -- C Programming · Structure definition in C: โครงสร้างของ structure ... Common Programming Errors • Attempting to use structures and unions, as

Summary

• A structure allows individual variables to be grouped under a common variable name

• A structure type name can be used to create a generalized structure type describing the form and arrangement of elements in a structure

• Structures are particularly useful as elements of arrays

A First Book of ANSI C, Fourth Edition 25

Page 26: 204216 -- C Programming · Structure definition in C: โครงสร้างของ structure ... Common Programming Errors • Attempting to use structures and unions, as

Summary (2)

• Individual members of a structure are passed to a function in the manner appropriate to the data type of the member being passed

• Structure members can be any valid C data type, including structures, unions, arrays, and pointers

• Unions are declared in the same manner as structures

A First Book of ANSI C, Fourth Edition 26