บทที่ 6 สตรัคเจอร์ structure
DESCRIPTION
บทที่ 6 สตรัคเจอร์ Structure. วัตถุประสงค์. เพื่อให้นักศึกษารู้จักสตรัคเจอร์ เข้าใจรูปแบบการจัดเก็บ และนำสตรัคเจอร์ไปใช้งานได้ เพื่อให้นักศึกษาสามารถส่งผ่านค่าของสตรัคเจอร์ไปยังฟังก์ชัน และรับค่าของสตรัคเจอร์จากฟังก์ชันได้. สตรัคเจอร ์คืออะไร การนิยามสตรัคเจอร์และการประกาศตัวแปร - PowerPoint PPT PresentationTRANSCRIPT
Department of Computer Engineering, PSU 1
241-101 Introduction to Computer Programming
คณาจารยภ์าควชิาวศิวกรรมคอมพวิเตอร์[email protected]
บทท่ี บทท่ี 6 6 สตรคัเจอร์สตรคัเจอร์StructureStructure
241-101 Introduction to Computer Programming
Department of Computer Engineering, PSU 2
วตัถปุระสงค์ วตัถปุระสงค์
• เพื่อใหน้ักศึกษารูจ้กัสตรคัเจอร ์ เขา้ใจรูปแบบการจดัเก็บ และนำาสตรคัเจอรไ์ปใชง้านได้
• เพื่อใหน้ักศึกษาสามารถสง่ผ่านค่าของสตรคัเจอรไ์ปยงัฟงัก์ชนั และรบัค่าของสตรคัเจอรจ์ากฟงัก์ชนัได้
241-101 Introduction to Computer Programming
Department of Computer Engineering, PSU 3
หวัขอ้ศึกษาหวัขอ้ศึกษา• สตรคัเจอร์คืออะไร• การนิยามสตรคัเจอร์
และการประกาศตัวแปร
• การเขา้ถึงสมาชกิในสตรคัเจอร์
• การคัดลอกค่าตัวแปรสตรคัเจอร์
• สตรคัเจอรท่ี์มสีมาชกิเป็นสตรคัเจอร์
• การให้ค่าเริม่ต้นกับตัวแปรสตรคัเจอร์
• การเปรยีบเทียบค่าของตัวแปรสตรคัเจอร์
• การกำาหนดชนิดตัวแปรใหม่
• การผ่านสตรคัเจอรใ์ห้ฟงัก์ชนั
• การใชอ้ารเ์รยกั์บสตรคัเจอร์
241-101 Introduction to Computer Programming
Department of Computer Engineering, PSU 4
สตรคัเจอร ์สตรคัเจอร ์Structure Structure คืออะไรคืออะไรขอ้มูลพื้นฐาน (simple data ) เชน่ int, unsigned int, char,
float, double, long เป็นต้นขอ้มูลซบัซอ้น (complex data ) ได้แก่ array, structure &
unionสตรคัเจอร ์หรอืกลุ่มขอ้มูลชนิดโครงสรา้ง ( Structure / struct )
คือ - เป็นการประกาศหน่วยของขอ้มูลใหมท่ี่เกิดจากการรวมกลุ่มของขอ้มูล เป็นโครงสรา้ง โดยขอ้มูลซึ่งเป็นสมาชกิของโครงสรา้งใหม ่อาจมหีลายตัว และเป็นชนิดเดียวกันหรอืต่างชนิดกันก็ได้ เชน่ มีสมาชกิเป็นจำานวนเต็ม ทศนิยม และอักขระ ได้ - สตรคัเจอร ์อาจมสีมาชกิท่ีเป็นอารเ์รย ์หรอืแมแ้ต่สตรคัเจอรด์้วยก็ได้
241-101 Introduction to Computer Programming
Department of Computer Engineering, PSU 5
ประโยชน์สตรคัเจอร ์หรอืกลุ่มขอ้มูลชนิดโครงสรา้ง (Structure) คือ -เพื่อกำาหนดหน่วยขอ้มูลใหม ่ให้เหมาะสมกับขอ้มูลที่ต้องการเก็บ- เชน่ การกำาหนดหน่วยขอ้มูลนักเรยีน ท่ีประกอบด้วยสมาชกิเป็น ชื่อและนามสกลุท่ีเป็น string (char [ ]) กับรหสันักศึกษาท่ีเป็นตัวเลข(int) ซึ่งเราสามารถกำาหนดให้ตัวแปรนักเรยีน A กับ B มโีครงสรา้งแบบหน่วยขอ้มูลท่ีสรา้งขึ้นนี้ได้ดังรูป
Anna Arty
490001
BillyBallet
490002
NameSurname
ID
Structure of Student dataStudent_A Student_B
241-101 Introduction to Computer Programming
Department of Computer Engineering, PSU 6
การนิยามและการประกาศสตรคัเจอร์Structure definiton &
declarationการประกาศตัวแปรของกลุ่มขอ้มูลชนิดโครงสรา้ง structure ม ี2ขัน้ตอน1. Structure definition การนิยามกลุ่มขอ้มูลท่ีสรา้งใหม ่วา่มสีมาชกิอะไรบา้ง เป็นชนิดใด ขอ้มูลยอ่ยในสตรคัเจอรเ์รยีกวา่ ฟลิด์ (Field)2. Structure declaration ประกาศตัวแปรสำาหรบักลุ่มขอ้มูลท่ีสรา้งขึ้นมา
241-101 Introduction to Computer Programming
Department of Computer Engineering, PSU 7
การนิยามและการประกาศสตรคัเจอร(์ต่อ)การนิยามกลุ่มขอ้มูลท่ีสรา้งใหมว่า่มสีมาชกิขอ้มูลชนิดใด
บา้งรูปแบบ
struct ชื่อของสตรคัเจอร์ { ชนิดของตัวแปร ชื่อตัวแปร [, ชื่อตัวแปร , ...]; ชนิดของตัวแปร ชื่อตัวแปร [, ชื่อตัวแปร , ...]; ............ ชนิดของตัวแปร ชื่อตัวแปร [, ชื่อตัวแปร , ...];
} ชื่อตัวแปร [, ชื่อตัวแปร , ...] ;
241-101 Introduction to Computer Programming
Department of Computer Engineering, PSU 8
สมาชกิขอ้มูลภายในม ี4 ตัว(4 fields)
struct address{ char name[30];char detail[50];int age;char telephone[10];};
struct student{ char name[30];char surname[50];int Id;};
การนิยามและการประกาศสตรคัเจอร์(ต่อ) ตัวอยา่งการระบุกลุ่มขอ้มูลชนิดโครงสรา้งที่สรา้งขึน้มาใหม่
ชื่อของ Structure
241-101 Introduction to Computer Programming
Department of Computer Engineering, PSU 9
ตัวอยา่งท่ีตัวอยา่งท่ี 1.1. การนิยามสตรคัเจอร์การนิยามสตรคัเจอร์
จงนิยามสตรคัเจอรช์ื่อวา่ date เพื่อใชใ้นการเก็บขอ้มูลของวนัท่ีโดยประกอบด้วยสมาชกิ 3 ตัวชื่อวา่ day, month และ year ซึ่งสมาชกิทัง้สามเป็นจำานวนเต็ม
struct date{ int day; int month; int year;};
หรอืstruct date { int day,month,year;};
241-101 Introduction to Computer Programming
Department of Computer Engineering, PSU 10
แบบฝึกหดัแบบฝึกหดั
จงนิยามสตรคัเจอรช์ื่อวา่ person ซึ่งมี สมาชกิ 2 ตัวคือ name ใชส้ำาหรบัเก็บ
ขอ้ความท่ียาวไมเ่กิน 50 ตัวอักษร และ age ซึ่งเป็นจำานวนเต็มstruct person { char name[50]; int age;}
241-101 Introduction to Computer Programming
Department of Computer Engineering, PSU 11
การนิยามและการประกาศสตรคัเจอร(์ต่อ)
• ประกาศตัวแปรสำาหรบักลุ่มขอ้มูลท่ีสรา้งขึ้นมามวีธิกีารประกาศได้ 2 ลักษณะคือ
1. การประกาศโดยตรง (ภายหลังจากมกีารนิยามสตรคัเจอรแ์ล้ว) โดยมรีูปแบบ
struct ชื่อแบบของสตรคัเจอร ์ ชื่อตัวแปร [, ชื่อตัวแปร , ...];เชน่ struct student stdA, stdB; 2. การประกาศตัวแปรพรอ้มการสรา้งกลุ่มขอ้มูลโดยการระบุต่อท้ายก่อนเครื่องหมาย ;
241-101 Introduction to Computer Programming
Department of Computer Engineering, PSU 12
การนิยามและการประกาศสตรคัเจอร(์ต่อ)
การประกาศโดยตรง โดยมรูีปแบบstruct ชื่อแบบของสตรคัเจอร ์ ชื่อ
ตัวแปร [, ชื่อตัวแปร , ...];
ตัวอยา่ง struct address input1, input2;
address input1, input2;
241-101 Introduction to Computer Programming
Department of Computer Engineering, PSU 13
การนิยามและการประกาศสตรคัเจอร(์ต่อ)
2. การประกาศตัวแปรพรอ้มการสรา้งกลุ่มขอ้มูลโดยการระบุต่อท้ายก่อนเครื่องหมาย ; ตัวอยา่งstruct address{ char name[30];char detail[50];int age;char telephone[10];} input1, input2 ;
หลังจากน้ีอาจมกีารประกาศตัวแปรเพิม่ได้ เชน่ struct address input3, input4 ;
ถ้าชื่อแบบของสตรคัเจอรไ์มม่กีารนำาไปใชอี้ก อาจไมม่กีารระบุก็ได้
241-101 Introduction to Computer Programming
Department of Computer Engineering, PSU 14
การเขา้ถึงสมาชกิในสตรคัเจอร ์ สำาหรบั Array เราใชเ้ลขดัชนีหรอื index(subscript) ในการอ้างอิง สมาชกิแต่ละตัวใน array (เชน่ a[5], b[1][0] เป็นต้น) แต่รูปแบบการเขา้ถึงขอ้มูลสมาชกิของ struct ทำาได้โดยใชโ้อเปอเรเตอรจุ์ด (.) แล้วตามด้วยชื่อสมาชกิ
รูปแบบ: ชื่อตัวแปรสตรคัเจอร.์ชื่อสมาชกิ structure-variable.field
241-101 Introduction to Computer Programming
Department of Computer Engineering, PSU 15
การเขา้ถึงสมาชกิในสตรคัเจอร ์
จากรูปแบบ: ชื่อตัวแปรสตรคัเจอร.์ชื่อสมาชกิ• ตัวอยา่ง
struct complex { double real; double imag; } num0 ;
struct complex num1;
การกำาหนดค่า เชน่num1.real = 1;num1.imag = 2.5;
Num0.real = num1.imag;num0.imag = num1.real;
241-101 Introduction to Computer Programming
Department of Computer Engineering, PSU 16
ตัวอยา่งการเขยีนโค้ดภาษาซเีพื่อจดัการกับ ตัวเลขเชงิซอ้น Complex number1.นิยามกลุ่มขอ้มูลชนิดเชงิซอ้นstruct comp { float real; float imag; }; 2.ประกาศตัวแปร struct comp a,b,c;
3.ตัวอยา่งการใชง้านตัวแปรc.real=a.real+b.real;c.imag=a.imag+b.imag;
Structure Assignment การกำาหนดค่าด้วยสตรคัเจอร์a = b; มคีวามหมายเชน่เดียวกับa.real=b.real; a.imag=b.imag; คือcopyทกุขอ้มูลจาก b ให้กับ aแต่ไมม่กีารเปรยีบเทียบโดยตรงระหวา่งสตรคัเจอร ์เชน่ a == b หรอื a>b หรอื a<b ใชไ้มไ่ด้
241-101 Introduction to Computer Programming
Department of Computer Engineering, PSU 17
ตัวอยา่งท่ี 2. การให้ค่ากับสมาชกิarrayแบบ char
struct employ { char name[25]; int age;
int pay; } employee;
การกำาหนดค่าemployee.age=32;employee.name[2] = 'X';
241-101 Introduction to Computer Programming
Department of Computer Engineering, PSU 18
ตัวอยา่งท่ี 3. การให้ค่าแบบstring //exam7_3.c
struct address{ char name[30];
char detail[50];int age;char telephone[10];
};struct address input;strcpy(input.name, "User Example");strcpy(input.detail, "1 ratutid Haddyai Songkhla");input.age=20;strcpy(input.telephone, "212895");
241-101 Introduction to Computer Programming
Department of Computer Engineering, PSU 19
แบบฝึกหดัแบบฝึกหดั จงเขยีนผลลัพธข์องโปรแกรม จงเขยีนผลลัพธข์องโปรแกรมต่อไปน้ีต่อไปน้ี
#include <stdio.h>int main(){ struct complex { double re; double im; } x,y; x.re = 4; x.im = 0.5; y.re = x.re + 1; y.im = x.im + 0.25; printf(“y= %.2f + %.2fi\n,y.re,y.im); return 0;}
reim
x
reim
y5
0.75 ผลลัพธ์y = 5.00 + 0.75i
40.5
241-101 Introduction to Computer Programming
Department of Computer Engineering, PSU 20
แบบฝึกหดั จงเติมสว่นท่ีขาดหายไปเพื่อใหไ้ด้ผลลัพธต์ามท่ีกำาหนด#include <stdio.h>#include <string.h>int main() { struct person { char name[30]; int age; char tel[10]; } p1; _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ // 1. _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ // 2. _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ // 3. printf("%s, %d years old.\n",p1.name,p1.age); printf("Telephone: %s\n",p1.tel); return 0;
ผลลัพธ์Pongsit Kampee, 36 years old.Telephone: 074212895
strcpy(p1.name,"Pongsit Kampee"); p1.age = 36; strcpy(p1.tel,"074212895");
241-101 Introduction to Computer Programming
Department of Computer Engineering, PSU 21
การคัดลอกค่าของตัวแปรสตรคัเจอร์การคัดลอกค่าของตัวแปรสตรคัเจอร์• การคัดลอก (Copy) ค่าของตัวแปรสตรคัเจอรช์นิด
เดียวกัน สามารถทำาได้โดยใชเ้ครื่องหมาย " = " เชน่
struct date { int day,month,year; } d1, d2;
• แทนท่ีจะกำาหนดค่าสมาชกิทีละตัว d2.day = d1.day; d2.month = d1.month; d2.year = d1.year;
• สามารถกำาหนดค่าเป็นตัวแปรสตรคัเจอรไ์ด้เลย d2 = d1;
241-101 Introduction to Computer Programming
Department of Computer Engineering, PSU 22
แบบฝึกหดัแบบฝึกหดั จงเขยีนผลลัพธส์ว่นท่ีเหลือ จงเขยีนผลลัพธส์ว่นท่ีเหลือ ((ขอ้ความสแีดงขอ้ความสแีดงคืออินพุตจากผู้ใช้คืออินพุตจากผู้ใช้))#include <stdio.h>#include <string.h>int main() { struct subject { char name[30]; int credit; } s1,s2; printf("Enter subject name: "); gets("%s",s1.name); printf("Enter credit: "); scanf("%d",&s1.credit); s2 = s1; s2.credit = s2.credit + 1; printf("%s, credit: %d\n",s2.name,s2.credit); return 0;
ผลลัพธ์Enter subject name: Physics IEnter credit: 3Physics I, credit: 4
Physics I3
namecredit
s1
namecredit
s2Physics I
34
241-101 Introduction to Computer Programming
Department of Computer Engineering, PSU 23
สตรคัเจอรท่ี์มสีมาชกิเป็นสตรคัเจอร์สตรคัเจอรท่ี์มสีมาชกิเป็นสตรคัเจอร์
struct date { int day; int month; int year;};struct person { char name[30]; struct date birthday;};
struct person p1; trcpy(p1.name,"Somchai");
p1.birthday.day = 10; p1.birthday.month = 3; p1.birthday.year = 1992;
241-101 Introduction to Computer Programming
Department of Computer Engineering, PSU 24
• ใชเ้ครื่องหมาย { } ครอ่มค่าเริม่ต้นทัง้หมด และใชเ้ครื่องหมาย , คัน่ระหวา่งค่าของสมาชกิแต่ละตัว ตัวอยา่งเชน่
struct date { int day; int month; int year; } ; struct date d1 = {10,5,1992};
การกำาหนดค่าเริม่ต้นใหกั้บตัวแปรสตรคัการกำาหนดค่าเริม่ต้นใหกั้บตัวแปรสตรคัเจอร์เจอร์
day 10month 5
d1
year 1992
241-101 Introduction to Computer Programming
Department of Computer Engineering, PSU 25
การกำาหนดค่าเริม่ต้นใหกั้บตัวแปรสตรคัการกำาหนดค่าเริม่ต้นใหกั้บตัวแปรสตรคัเจอร ์เจอร ์((ต่อต่อ))
struct subject { char name[20]; int credit; char grade; } s1 = {"Physics I",3,'A'};
name Physics Icredit 3
s1
grade A
241-101 Introduction to Computer Programming
Department of Computer Engineering, PSU 26
ตัวอยา่งท่ี 4. printf กับ ตัวแปร struct ท้ังหมด#include <stdio.h> //exam7_4.cint main(){ struct {
char name[30]; char detail[50]; int age; char telephone[10]; } address, newAddress = {“newUser",
"1 ratutid Haddyai Songkhla",20,"212895"};
/* Display address of data groups */ printf("Address of address variable is %p \n", &address); printf("Address of newAddress variable is %p \n", &newAddress); printf(“sizeof of newAddress = %d\n”,sizeof newAddress); return 0;}
ผลลัพธ์Address of address:0022FF10Address of address:0022FEB0sizeof newAddress = 96
241-101 Introduction to Computer Programming
Department of Computer Engineering, PSU 27
แบบฝึกหดั จงเติมสว่นท่ีขาดหายไปเพ่ือใหไ้ด้ผลลัพธต์ามท่ีกำาหนด
#include <stdio.h>// exer7_4.cint main() { struct date { int day, month, year; }; struct student { char name[30]; struct date bday; }; struct student std = _ _ _ _ _ _ _ _ _ _ _ _ printf("%s \n", std.name);
printf("Birthday: %d/%d/%d\n", std.bday.day, std.bday.month, std.bday.year); return 0;}
ผลลัพธ์Warakorn SuwannaratBirthday: 15/5/1980
{"Warakorn Suwannarat",{15,5,1980}};
241-101 Introduction to Computer Programming
Department of Computer Engineering, PSU 28
การเปรยีบเทียบค่าของตัวแปรสตรคัเจอร์การเปรยีบเทียบค่าของตัวแปรสตรคัเจอร์• ในการเปรยีบเทียบค่าของตัวแปรสตรคัเจอรน์ัน้ ให้
เปรยีบเทียบค่าของสมาชกิแต่ละตัว• จะนำาตัวแปรสตรคัเจอรส์ตรคัเจอรม์าเปรยีบเทียบ
กันโดยตรงไมไ่ด้ เชน่ หากมกีารตัวแปรชนิด struct date ชื่อวา่ d1 และ 2d
struct date { int day,month,year } d1,d2;
• จะนำา d1 และ d2 มาเปรยีบเทียบกันโดยตรงไมไ่ด้ if(d1 == d2)
printf(“d1 is the same date as d2”);
241-101 Introduction to Computer Programming
Department of Computer Engineering, PSU 29
ตัวอยา่งท่ี ตัวอยา่งท่ี 5.5.#include <stdio.h>int main(){ struct complex { double re,im; }; struct complex y,x = {1,0.25}; printf("Enter complex number: "); scanf("%lf%lf",&y.re,&y.im); _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ // 1. printf("x and y is the same complex.\n"); _ _ _ _ _ _ // 2. printf("x and y is not the same complex.\n"); return 0;}
จงเติมสว่นท่ีขาดหายไปเพื่อใช้ในการเปรยีบเทียบวา่
จำานวนเชงิซอ้นท่ีรบัจากผู้ใช้(y) วา่มค่ีาเท่ากับ x หรอืไม่
if((x.re == y.re) && (x.im == y.im))
else
241-101 Introduction to Computer Programming
Department of Computer Engineering, PSU 30
การกำาหนดชนิดตัวแปรใหม ่การกำาหนดชนิดตัวแปรใหม ่Type Type DefinitionDefinition• ในภาษาซ ีสามารถกำาหนดชนิดตัวแปรขึ้นมาใหมไ่ด้ โดยใช้
คำาสัง่ typedef (type definition)• รูปแบบ: typedef ชนิดตัวแปรท่ีมอียูแ่ล้ว ชนิดตัวแปร
ใหม;่ เชน่ typedef int my_int;• รูปแบบการใช ้typedef รว่มกับการนิยามสตรคัเจอร:์
typedef struct {
ชนิดตัวแปร ชื่อตัวแปรท่ี 1; ชนิดตัวแปร ชื่อตัวแปรท่ี 2;... ชนิดตัวแปร ชื่อตัวแปรท่ี n;} ชนิดตัวแปรใหม;่
241-101 Introduction to Computer Programming
Department of Computer Engineering, PSU 31
การกำาหนดชนิดตัวแปรใหม ่การกำาหนดชนิดตัวแปรใหม ่((ต่อต่อ))
• ตัวอยา่งเชน่ typedef struct { int day,month,year; } date;
• ในการประกาศตัวแปรก็สามารถใชช้นิดตัวแปรใหมไ่ด้เลย เชน่
date d1,d2;ขอ้สงัเกต เมื่อกำาหนดตัวแปรหลังจากการกำาหนดด้วย typedef แล้ว ไมต้่องมคีำาวา่ struct นำาหน้าชนิดขอ้มูลอีก
241-101 Introduction to Computer Programming
Department of Computer Engineering, PSU 32
ตัวอยา่งท่ี ตัวอยา่งท่ี 6.6.
จงกำาหนดชนิดตัวแปรใหมช่ื่อ Student ซึ่งมี สมาชกิ 2 ตัวคือ name ใชส้ำาหรบัเก็บชื่อซึ่งมี
ความยาวไมเ่กิน 30 ตัวอักษร และสมาชกิตัวท่ีสองชื่อ faculty ใชส้ำาหรบัเก็บชื่อคณะซึ่งมคีวามยาวไมเ่กิน 15 ตัวอักษร typedef struct
{ char name[31]; char faculty[16];} Student;
241-101 Introduction to Computer Programming
Department of Computer Engineering, PSU 33
struct info{char firstName[20];char lastName[20];int age;};
struct info i1, i2;
info j;
typedef struct {char firstName[20];char lastName[20];int age;} Info; Info j;
struct info k;
ตัวอยา่งท่ี 7. การใช ้typedef กับ struct
typedef struct info infoType;
infoType i3,i4;
241-101 Introduction to Computer Programming
Department of Computer Engineering, PSU 34
typedef struct { char firstName[20]; char lastName[20]; int age; } InfoT; typedef struct { InfoT info; double salary; } EmployeeT;
EmployeeT e1; e1.info.age = 21;
ตัวอยา่งท่ี ตัวอยา่งท่ี 8.8.typedef typedef ท่ีมสีมาชกิเป็น ท่ีมสีมาชกิเป็น typedef typedef
241-101 Introduction to Computer Programming
Department of Computer Engineering, PSU 35
ตัวอยา่งท่ี ตัวอยา่งท่ี 9.9. การเปรยีบเทียบค่าและการถ่ายโอนค่าการเปรยีบเทียบค่าและการถ่ายโอนค่า
จากตัวอยา่งท่ี 8 InfoT i1, i2;
การให้ค่าสามารถทำาได้โดยตรงi1 = i2;
แต่การเปรยีบเทียบไมส่ามารถทำาได้โดยตรง i1 == i2;
ดังนัน้การเปรยีบเทียบสามารถกระทำาได้โดยif (strcmp(i1.firstName, i2.firstName) == 0 &&strcmp(i1.lastName, i2.lastName) == 0 && (i1.age == i2.age)
) printf(“i1 is equal to i2\n”);
241-101 Introduction to Computer Programming
Department of Computer Engineering, PSU 36
การผ่านสตรคัเจอรใ์ห้กับฟงัก์ชนัการผ่านสตรคัเจอรใ์ห้กับฟงัก์ชนั
• การผ่านค่าของตัวแปรสตรคัเจอรใ์ห้กับฟงัก์ชนัทำาได้เหมอืนกับตัวแปรชนิดอ่ืนๆ (int, float, char)
• การแก้ไขของพารามเิตอรภ์ายในฟงัก์ชนั จะไมม่ผีลต่อค่าของตัวแปรสตรคัเจอรท่ี์ถกูสง่มาเป็นอารกิ์วเมนต์
• ถ้าในโปรแกรมมหีลายฟงัก์ชนั การนิยามสตรคัเจอร์และการกำาหนดชนิดตัวแปรใหมใ่ห้นำามาไวน้อกฟงัก์ชนั main
241-101 Introduction to Computer Programming
Department of Computer Engineering, PSU 37
ตัวอยา่งท่ี ตัวอยา่งท่ี 11 00.. จงเขยีนผลลัพธข์องจงเขยีนผลลัพธข์องโปรแกรมต่อไปนี้โปรแกรมต่อไปนี้#include <stdio.h> //exam7_10.c
typedef struct { double re; double im;} complex;void display(complex a);int main() { complex x; x.re = 1; x.im = 0.75; display(x); return 0;}void display(complex a) { printf("%.2f + %.2fi\n",a.re,a.im);}
re 1im 0.75
x
reim
a
ผลลัพธ์1.00 + 0.75i
10.75
241-101 Introduction to Computer Programming
Department of Computer Engineering, PSU 38
ตัวอยา่งท่ี ตัวอยา่งท่ี 11 11.. จงเขยีนผลลัพธข์องโปรแกรมต่อไปนี้จงเขยีนผลลัพธข์องโปรแกรมต่อไปนี้#include <stdio.h>typedef struct { int day,month,year;} date;void edit(date a);int main() { date d1 = {15,10,1992}; edit(d1); printf("%d/%d/%d\n",d1.day,d1.month,d1.year); return 0;}void edit(date a){ a.year = a.year + 10;}
ผลลัพธ์15/10/1992
การแก้ไขของพารามเิตอร์ ภายในฟงัก์ชนั จะไมม่ผีลต่อ
ค่าของตัวแปรสตรคัเจอรท่ี์ถกูสง่มาเป็นอารกิ์วเมนต์
241-101 Introduction to Computer Programming
Department of Computer Engineering, PSU 39
ตัวอยา่งท่ี ตัวอยา่งท่ี 11 22.. ( (ฟงัก์ชนัท่ีมกีารสง่ค่าฟงัก์ชนัท่ีมกีารสง่ค่ากลับเป็นสตรคัเจอร์กลับเป็นสตรคัเจอร์))#include <stdio.h>
typedef struct { double re,im;} complex;complex cconst(double a,double b);int main() { double x = 2,y = 0.5; complex cnum; cnum = cconst(x,y); printf("%.2f + %.2fi\n",cnum.re,cnum.im); return 0;}complex cconst(double a,double b) { complex num; num.re = a; num.im = b; return num; }
reim
cnum
reim
num
ผลลัพธ์2.00 + 0.50i
20.5
20.5
241-101 Introduction to Computer Programming
Department of Computer Engineering, PSU 40
การใชอ้ารเ์รยกั์บสตรคัเจอร์การใชอ้ารเ์รยกั์บสตรคัเจอร์• ในเก็บขอ้มูลท่ีต้องใชตั้วแปรสตรคัเจอรจ์ำานวนมาก
สามารถแก้ปัญหาโดยการใชตั้วแปรอารเ์รยข์องสตรคัเจอร์
struct date { int day,month,year; }; struct date date_list[3];
yeardate_list[0]
daymonthyear
daymonthyear
daymonth
date_list[1] date_list[2]
241-101 Introduction to Computer Programming
Department of Computer Engineering, PSU 41
การใชอ้ารเ์รยกั์บสตรคัเจอร ์การใชอ้ารเ์รยกั์บสตรคัเจอร ์((ต่อต่อ))
• รูปแบบการเขา้ถึงสมาชกิในแต่ละอีลีเมนต์ในอารเ์รยข์องสตรคัเจอร์ชื่อตัวแปรอารเ์รยข์องสตรคัเจอร[์ดัชนี].ชื่อสมาชกิ
• ตัวอยา่งเชน่ (กำาหนดค่าใหกั้บสมาชกิในอีลีเมนต์แรก)
date_list[0].day = 12; date_list[0].month = 10; date_list[0].year = 2002;
241-101 Introduction to Computer Programming
Department of Computer Engineering, PSU 42
ตัวอยา่งท่ีตัวอยา่งท่ี 1313.. ( (การรบัค่าและแสดงค่าการรบัค่าและแสดงค่าอารเ์รยข์องสตรคัเจอร์อารเ์รยข์องสตรคัเจอร์))#include <stdio.h>
int main() { typedef struct { char name[30]; int age; } student; student stds[3]; int i; for(i=0;i<3;i++) { printf("Enter name of student %d: ",i+1); scanf("%s",stds[i].name); printf("Enter age: "); scanf("%d",&stds[i].age); } printf("==================\n"); printf("Name Age\n"); printf("==================\n"); for(i=0;i<3;i++) printf("%-15s%d\n",stds[i].name, stds[i].age); return 0;}
241-101 Introduction to Computer Programming
Department of Computer Engineering, PSU 43
ผลลัพธ์Enter name of student 1: JohnEnter age: 14Enter name of student 2: DavidEnter age: 15Enter name of student 3: AngieEnter age: 12==================Name Age==================John 14David 15Angie 12
241-101 Introduction to Computer Programming
Department of Computer Engineering, PSU 44
#include <stdio.h>typedef struct { char name[30]; int salary;} employee;void display (employee emps[]);int main(){ employee emp_list[3]; int i; for(i=0;i<3;i++) { printf("Enter name of employee %d: ",i+1); scanf("%s", emp_list[i].name );
printf("Enter salary: ");scanf("%d", &emp_list[i].salary );
} display(emp_list); return 0;}
ตัวอยา่งท่ี ตัวอยา่งท่ี 1414.. ( (ฟงัก์ชนัที่มกีารรบัค่าเขา้เป็นฟงัก์ชนัที่มกีารรบัค่าเขา้เป็นอารเ์รยข์องสตรคัเจอร์อารเ์รยข์องสตรคัเจอร์))
ฟงัก์ชนั display รบัค่า เขา้เป็นอารเ์รยข์อง
employee
241-101 Introduction to Computer Programming
Department of Computer Engineering, PSU 45
void display(employee emps[]){ int i; printf("---------------------\n"); printf("Name Salary\n"); printf("---------------------\n"); for(i=0;i<3;i++) printf("%-15s%d \n", emps[i].name,
emps[i].salary);
}
241-101 Introduction to Computer Programming
Department of Computer Engineering, PSU 46
ผลลัพธ์Enter name of employee 1: SomsakEnter salary: 12000Enter name of employee 2: AnanEnter salary: 15000Enter name of employee 3: SomsriEnter salary: 9000---------------------Name Salary---------------------Somsak 12000Anan 15000Somsri 9000
241-101 Introduction to Computer Programming
Department of Computer Engineering, PSU 47
ขนาดของตัวแปรสตรคัเจอร์ขนาดของตัวแปรสตรคัเจอร์
• ขนาดของตัวแปรสตรคัเจอรม์ค่ีาเท่ากับขนาดของสมาชกิแต่ละตัวมารวมกัน เชน่
struct person { char name[20]; int age; }; struct person p1;
• ขนาดของตัวแปร p1 = 20 + 4 = 24 ไบต์
241-101 Introduction to Computer Programming
Department of Computer Engineering, PSU 48
ตัวอยา่งท่ี ตัวอยา่งท่ี 1515.. จงเขยีนผลลัพธข์องจงเขยีนผลลัพธข์องโปรแกรมต่อไปน้ีโปรแกรมต่อไปน้ี
#include <stdio.h>int main() { struct date { int day,month,year; } d1; struct person { int age; char name[25]; } p1; printf("size of d1 = %d\n",sizeof(d1)); printf("size of p1 = %d\n",sizeof(p1)); return 0; }
ผลลัพธ์size of d1 = 12size of p1 = 32
241-101 Introduction to Computer Programming
Department of Computer Engineering, PSU 49
คำาถาม คำาถาม ??
• ทำาไมขนาดของตัวแปร p1 จงึได้เท่ากับ 32 แทนท่ีจะเป็น 29?
• วธิกีารจองหน่วยความจำาของคอมไพเลอร์
• ****สำาหรบัคอมไพเลอร ์GCC ไมว่า่จะเป็นตัวแปรสตรคัเจอรห์รอืยูเนียน มขีอ้กำาหนดวา่การจองหน่วยความจำาจะต้องมขีนาดท่ีหารด้วย 4 ลงตัวเสมอ
241-101 Introduction to Computer Programming
Department of Computer Engineering, PSU 50
จากตัวอยา่งท่ี 14 จงแก้ไขโปรแกรมให้ผู้ใชส้ามารถกำาหนดจำานวนพนักงานได้ (สมมุติวา่จำานวนพนักงานไมเ่กิน 10 คน)
ตัวอยา่งผลลัพธข์องโปรแกรมEnter number of employees: 2Enter name of employee 1: SawatEnter salary: 20000Enter name of employee 2: SamritEnter salary: 16000---------------------Name Salary---------------------Sawat 20000Samrit 16000
แบบฝึกหัด
241-101 Introduction to Computer Programming
Department of Computer Engineering, PSU 51
#include <stdio.h>typedef struct { char name[30]; int salary;} employee;void display (employee emps[],int n);int main(){ employee emp_list[10]; int i, n; printf(“Enter number of emp: ”); scanf(”%d”,&n); for(i=0;i<n;i++) { printf("Enter name of employee %d: ",i+1); scanf("%s", emp_list[i].name );
printf("Enter salary: "); scanf("%d", &emp_list[i].salary );} display(emp_list); return 0;}
ตัวอยา่งท่ี ตัวอยา่งท่ี 1414.. – –แก้ไข เพิม่เติมแก้ไข เพิม่เติม ฟงัก์ชนั display รบัค่า
เขา้เป็นอารเ์รยข์องemployee
241-101 Introduction to Computer Programming
Department of Computer Engineering, PSU 52
void display(employee emps[],int n){ int i; printf("---------------------\n"); printf("Name Salary\n"); printf("---------------------\n"); for(i=0;i<n;i++) printf("%-15s%d \n", emps[i].name,
emps[i].salary);
}