230 likes | 340 Views
บทที่ 12 Structure and union Kairoek choeychuen. ใช้เก็บข้อมูลแบบโครงสร้าง (structure) ข้อมูลแบบโครงสร้างคือกลุ่มข้อมูลที่อาจสัมพันธ์ หรือไม่สัมพันธ์กันก็ได้เช่น ข้อมูลพนักงานของบริษัทแห่งหนึ่ง. Structure คืออะไร. รูปแบบ 1 struct struct_name{ data_type1 var_name1;
E N D
ใช้เก็บข้อมูลแบบโครงสร้าง (structure) • ข้อมูลแบบโครงสร้างคือกลุ่มข้อมูลที่อาจสัมพันธ์ • หรือไม่สัมพันธ์กันก็ได้เช่น • ข้อมูลพนักงานของบริษัทแห่งหนึ่ง Structure คืออะไร
รูปแบบ 1 struct struct_name{ data_type1 var_name1; data_type2 var_name2; … data_type n var_nanme n; } ref_struct_name;
ตัวอย่างรูปแบบ 1 struct employee{ char name[10]; char surname[13]; char sex[6]; int age; char position; int salary; } emp;
รูปแบบ 2 struct struct_name{ data_type1 var_name1; data_type2 var_name2; … data_type n var_nanme n; }; struct struct_name ref_struct_name;
ตัวอย่างรูปแบบ 2 struct employee{ char name[10]; char surname[13]; char sex[6]; int age; char position; int salary; }; Struct employee emp;
คีย์เวิร์ด typedef - ใช่สร้างชนิดข้อมูลเฉพาะ ที่ผู้เขียนโปรแกรมต้องการ รูปแบบ1: ชนิดข้อมูลปกติ typedef data_type name; รูปแบบ2: ชนิดข้อมูลโครงสร้าง typedef struct{ data_type1 var_name1; … data_type n var_nanme n; }struct_name; struct_name ref_struct_name; คีย์เวิร์ด typedef
ตัวอย่างรูปแบบ1: ชนิดข้อมูลปกติ typedef int NUM; typedef char CHA; ตัวอย่างรูปแบบ2: ชนิดข้อมูลโครงสร้าง typedef struct{ char student_id[10]; int points; float grade; }STUDENT; STUDENT std; คีย์เวิร์ด typedef
ตัวอย่าง การกำหนดค่าเริ่มต้นให้ ตัวแปรชนิด struct ตัวอย่างที่ 1 struct person{ char name[10]; int age;}; struct person ps = {“Somchai”, 21}; struct รูปแบบที่ 2
ตัวอย่าง การกำหนดค่าเริ่มต้นให้ ตัวแปรชนิด struct ตัวอย่างที่ 2 struct rentroom{ int roomNum; char res_name[10]; char sex; }room1 = {1,“Somchai”}; หมายเหตุ: sex ไม่ถูกกำหนดค่าเริ่มต้นจะเก็บค่า default , เช่น sex เก็บ \0 struct รูปแบบที่ 1
ตัวอย่าง การกำหนดค่าเริ่มต้นให้ ตัวแปรชนิด struct ตัวอย่างที่ 3 typedef struct { int roomNum; char res_name[10]; char sex; }rentroom; rentroom rr = {1,“Somying”, ‘F’}; struct รูปแบบ typedef
การอ่านและเขียนข้อมูล แบบโครงสร้าง รูปแบบการอ่านฯ ตัวแปรรับค่า = ref_name_struct.data_type; รูปแบบการเขียนฯ ref_name_stract.data_type = ข้อมูล; Read and write structured data
เขียนข้อมูล ใส่ rr Run program อ่านข้อมูล จาก rr หมายเหตุ: //atoi แปลง สตริง เป็น จำนวนเต็ม #รูปแบบ: จำนวนเต็ม = atoi(สตริง); //atof แปลง สตริง เป็น จำนวนจริง #รูปแบบ: จำนวนจริง = atof(สตริง);
การคัดลอกข้อมูลระหว่าง สตรัคเจอร์ struct number{ int a; char b; float c; }num1 = {1, ‘a’, 1.1}, num2 = {2, ‘b’, 2.2}; คัดลอกข้อมูลจาก num2 ไป num1 num1 = num2;
สตรัคเจอร์ซ้อนสตรัคเจอร์ (Nested structure) struct time{ int hour; int min; int sec; }; กำหนดค่าเริ่มต้น สตรัค time ใน สตรัค sms ด้วย สัญลักษณ์ {…} struct sms s = {“Kairoek”, “Hello”, {16,54,55}}; เปลี่ยนค่า hour ใน สตรัค time ของ สตรัค smsและค่า msgใน สตรัค sms struct sms{ char sender[10]; char msg[40]; struct time t; }; s.t.hour = 17; strcpy(s.msg, “Hi”); ประกาศ สตรัค time ใน สตรัค sms
อาร์เรย์ของสตรัคเจอร์อาร์เรย์ของสตรัคเจอร์ struct number{ int a; char b; float c; }num1 = {1, ‘a’, 1.1}, num2 = {2, ‘b’, 2.2}; struct number{ int a; char b; float c; }num[2] = {{1, ‘a’, 1.1}, {2, ‘b’, 2.2}}; ประกาศ อาร์เรย์ของสตรัค num จำนวน 2 ตัวพร้อมกำหนดค่าเริ่มต้น
การกำหนดค่าให้อาร์เรย์ของสตรัคเจอร์การกำหนดค่าให้อาร์เรย์ของสตรัคเจอร์ struct number{ int a; char b; float c; }num[2] = {{1, ‘a’, 1.1}, {2, ‘b’, 2.2}}; num[0].a = 11; num[0].b = ‘c’; num[0].c = 1.2; num[1].a = 12; num[1].b = ‘d’; num[1].c = 11; int i; for (i = 0;i<2;i++) { scanf("%d",&num[i].a); getche(num[i].b); scanf("%f",&num[i].c); } หรือ
พอยน์เตอร์ของสตรัคเจอร์พอยน์เตอร์ของสตรัคเจอร์ typedef struct{ int a; char b; } TEST; TEST t = {1, ‘a’}; TEST *pt; pt = &t; t.a = 2; (*pt).a = 3; pt->a = 4; กำหนด พอยน์เตอร์pt เป็นชนิด TEST กำหนด พอยน์เตอร์pt ชี้ที่ สตรัค t กำหนดค่าให้กับ สมาชิก a ของสตรัค t ทำได้ 3 แบบ
ฟังก์ชันที่มีการรับค่าและส่งค่าของสตรัคเจอร์ฟังก์ชันที่มีการรับค่าและส่งค่าของสตรัคเจอร์ รูปแบบฟังก์ชันโปรโตไทป์ structชื่อสตรัค ชื่อฟังก์ชัน(struct ชื่อสตรัค) รูปแบบฟังก์ชัน structชื่อสตรัค ชื่อฟังก์ชัน(structชื่อสตรัค ชื่อตัวแปรชนิดสตรัค)
ฟังก์ชันที่มีการรับค่าและส่งค่าของสตรัคเจอร์ฟังก์ชันที่มีการรับค่าและส่งค่าของสตรัคเจอร์ #include<stdio.h> #include<conio.h> struct data change(struct data); struct data{ int a; char b;}d = {1, ‘A’}; main(){ printf(“Before\n”); printf(“a = %d\n”, d.a); printf(“b = %c\n”, d.b); d = change(d); ต่อ printf(“after\n”); printf(“a = %d\n”, dd.a); printf(“b = %d\n”, dd.b); struct data change(stract data dt){ dt.a = 2; dt.b = ‘B’; return dt; }
Union (ยูเนียน) - เป็นข้อมูลชนิดโครงสร้างเช่นเดียวกับ structure - สมาชิกแต่ละตัวใน ยูเนียน ใช้หน่วยความจำร่วมกัน structure union union u1{ int a; float b; char c[5]; }uu1; struct s1{ int a; float b; char c[5]; }ss1;
structure struct s1{ int a; float b; char c[5]; }ss1; หน่วยความจำ a b c union หน่วยความจำ union u1{ int a; float b; char c[5]; }uu1; a b c
#include<stdio.h> union number{ int x; double y; }; void main() { union number value; value.x = 100; value.y = 95578.25; printf(“Value of x = %d\n”, value.x); printf(“Value of y = %f\n”, value.y); }