Transcript Structure

บทที่ 12 STRUCTURE AND UNION
KAIROEK CHOEYCHUEN
Structure คืออะไร
-ใช้ เก็บข้ อมูลแบบโครงสร้ าง (structure)
-ข้ อมูลแบบโครงสร้ างคือกลุม่ ข้ อมูลที่อาจสัมพันธ์
หรื อไม่สมั พันธ์กนั ก็ได้ เช่น
ข้ อมูลพนักงานของบริษัทแห่ งหนึ่ง
ชื่อ
นามสกุล
เพศ
อายุ
ตาแหน่ง
เงินเดือน
Thomas
Tomasi
Male
55
Director
100,000
John
Paker
Male
45
Compute 19,000
r Eng.
รู ปแบบ 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};
num1 = num2;
คัดลอกข้อมูลจาก num2 ไป num1
สตรั คเจอร์ ซ้อนสตรั คเจอร์ (Nested structure)
struct time{
กาหนดค่าเริม่ ต้น สตรัค time ใน สตรัค
int hour;
sms ด้วย สัญลักษณ์ {…}
int min;
struct sms s = {“Kairoek”, “Hello”, {16,54,55}};
int sec;
};
เปลี่ยนค่า hour ใน สตรัค time ของ สตรัค
struct sms{
char sender[10]; sms และค่า msg ใน สตรัค sms
char msg[40];
s.t.hour = 17;
struct time t;
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{
ประกาศ อาร์เรย์ของสตรัค num
int a;
จานวน 2 ตัวพร้อมกาหนดค่าเริ่มต้น
char b;
float c;
}num[2] = {{1, ‘a’, 1.1}, {2, ‘b’, 2.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 ชื่อสตรัค ชื่อตัวแปรชนิดสตรัค)
ฟั งก์ ชันที่มีการรั บค่ าและส่ งค่ าของสตรั คเจอร์
printf(“after\n”);
#include<stdio.h>
ต่
อ
printf(“a = %d\n”, dd.a);
#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(“b = %d\n”, dd.b);
struct data change(stract data dt){
dt.a = 2;
dt.b = ‘B’;
return dt;
}
Union (ยูเนียน)
- เป็ นข้ อมูลชนิดโครงสร้ างเช่นเดียวกับ structure
- สมาชิกแต่ละตัวใน ยูเนียน ใช้ หน่วยความจาร่วมกัน
structure
struct s1{
int a;
float b;
char c[5];
}ss1;
union
union u1{
int a;
float b;
char c[5];
}uu1;
structure
struct s1{
int a;
float b;
char c[5];
}ss1;
union
union u1{
int a;
float b;
char c[5];
}uu1;
หน่ วยความจา
a
b
หน่ วยความจา
a
b
c
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);
}