บทที่ 11...
description
Transcript of บทที่ 11...
![Page 1: บทที่ 11 การเขียนโปรแกรมโดยใช้ข้อมูลชนิดพอยท์เตอร์](https://reader036.fdocuments.net/reader036/viewer/2022062722/56813aec550346895da35878/html5/thumbnails/1.jpg)
บทท�� 11 การเขี�ยนโปรแกรมโดยใช้�ขี�อม�ล
ช้น�ดพอยท�เตอร�
![Page 2: บทที่ 11 การเขียนโปรแกรมโดยใช้ข้อมูลชนิดพอยท์เตอร์](https://reader036.fdocuments.net/reader036/viewer/2022062722/56813aec550346895da35878/html5/thumbnails/2.jpg)
พอยท�เตอร� (Pointer)
พอยท�เตอร� คื�อ ต�วแปรซึ่!�งเก#บต$าแหน&งขีองอ�กต�วแปรหน!�ง ซึ่!�งจะม�ขีนาดตามต$าแหน&ง machine และพอยท�เตอร�ต�องช้�)ไปย�ง type ขีองขี�อม�ลท��แน&ช้�ด เป+นการเขี�าถึ!งขี�อม�ลแบบ indirectly
ร�ปแบบการประกาศtype *var;
![Page 3: บทที่ 11 การเขียนโปรแกรมโดยใช้ข้อมูลชนิดพอยท์เตอร์](https://reader036.fdocuments.net/reader036/viewer/2022062722/56813aec550346895da35878/html5/thumbnails/3.jpg)
พอยท�เตอร� (Pointer) (ต&อ)
เช้&น int *iptr; //เก#บต$าแหน&งขีองต�วแปรอ��นท��เป+น integer
char *cptr //เก#บต$าแหน&งขีองต�วแปรอ��นท��เป+น character
iptr integer var
cptr char var
![Page 4: บทที่ 11 การเขียนโปรแกรมโดยใช้ข้อมูลชนิดพอยท์เตอร์](https://reader036.fdocuments.net/reader036/viewer/2022062722/56813aec550346895da35878/html5/thumbnails/4.jpg)
ต�วด$าเน�นการ
ในภาษาซึ่� ได�ม�การก$าหนดต�วด$าเน�นการ 2 ช้น�ดท��เก��ยวก�บพอยท�เตอร� ด�งน�)
* หมายถึ!ง indirect operator
เป+นการประกาศใช้�ต�วแปรพอยท�เตอร�น�)นละย�งสามารถึใช้�เพ��อเขี�าถึ!งเน�)อท��ขีองหน&วยคืวามจ$าท��ต�วแปรน�)นช้�)
& หมายถึ!ง address operator
เป+นต�วด$าเน�นการท��ใช้�เพ��อบอกต$าแหน&งขีองต�วแปร แทนท��จะเป+นคื&าท��เก#บขี�อม�ล
![Page 5: บทที่ 11 การเขียนโปรแกรมโดยใช้ข้อมูลชนิดพอยท์เตอร์](https://reader036.fdocuments.net/reader036/viewer/2022062722/56813aec550346895da35878/html5/thumbnails/5.jpg)
ต�วด$าเน�นการ (ต&อ)
ต�วอย&าง เช้&นchar *ptr = NULL;
เป+น declaration : ท$าการจองเน�)อท��ให�ต�วแปรพอยท�เตอร�ช้��อ ptr และช้�)ไปย�ง NULL
*ptr = NULL;
เป+น executable code : การให�คื&า NULL แก&หน&วยคืวามจ$าต$าแหน&งท�� ptr ช้�)ไป
![Page 6: บทที่ 11 การเขียนโปรแกรมโดยใช้ข้อมูลชนิดพอยท์เตอร์](https://reader036.fdocuments.net/reader036/viewer/2022062722/56813aec550346895da35878/html5/thumbnails/6.jpg)
ต�วอย&าง 11.1 โปรแกรมท��ใช้�พอยท�เตอร�
main(){
int num = 3;int *ptr;
ptr =# printf(“The value of num is %d.
Its address is %lu.”,*ptr,(long)ptr);}
![Page 7: บทที่ 11 การเขียนโปรแกรมโดยใช้ข้อมูลชนิดพอยท์เตอร์](https://reader036.fdocuments.net/reader036/viewer/2022062722/56813aec550346895da35878/html5/thumbnails/7.jpg)
ผลล�พธ์�ขีองต�วอย&าง 11.1
ผลล�พธ์�>The value of num is 3. Its address is 448925078.
หมายเหตุ� คื&าต$าแหน&งขีองต�วแปรไม&แน&นอนในแต&ละคืร�)งขีองการส��งด$าเน�นการ
![Page 8: บทที่ 11 การเขียนโปรแกรมโดยใช้ข้อมูลชนิดพอยท์เตอร์](https://reader036.fdocuments.net/reader036/viewer/2022062722/56813aec550346895da35878/html5/thumbnails/8.jpg)
คื$าอธ์�บายโปรแกรม
Declaration: int num = 3;
int *ptr;
num
int var
ptr
Executable code: ptr = #
num
ptr
3
3
![Page 9: บทที่ 11 การเขียนโปรแกรมโดยใช้ข้อมูลชนิดพอยท์เตอร์](https://reader036.fdocuments.net/reader036/viewer/2022062722/56813aec550346895da35878/html5/thumbnails/9.jpg)
ต�วอย&าง 11.2 โปรแกรมท��ใช้�พอยท�เตอร�
main(){
static int number[6] = {1,2,3,4,5,6};int i,*ptr;
for(i=0;i<6;i++){ ptr = &number[i];
printf(“%d\n”,*ptr);}
}
![Page 10: บทที่ 11 การเขียนโปรแกรมโดยใช้ข้อมูลชนิดพอยท์เตอร์](https://reader036.fdocuments.net/reader036/viewer/2022062722/56813aec550346895da35878/html5/thumbnails/10.jpg)
ผลล�พธ์�โปรแกรมต�วอย&าง 11.2
ผลลพธ์ 1
2
3
4
5
6
หมายเหตุ� ถึ�าเราประกาศ int *ptr;เราสามารถึให� ptr ช้�)ไปย�งอาร�เรย�ได�ด�งน�) ptr = number;
หร�อ ptr = &number[0];
![Page 11: บทที่ 11 การเขียนโปรแกรมโดยใช้ข้อมูลชนิดพอยท์เตอร์](https://reader036.fdocuments.net/reader036/viewer/2022062722/56813aec550346895da35878/html5/thumbnails/11.jpg)
อธ์�บายโปรแกรม 11.2
1
2
3
4
5
6
number ptr 0
1
2
3
4
5
i = 0;
i = 1;
i = 2;
i = 3;
i = 4;
i = 5;
ptr
ptr
ptr
ptr
ptr
![Page 12: บทที่ 11 การเขียนโปรแกรมโดยใช้ข้อมูลชนิดพอยท์เตอร์](https://reader036.fdocuments.net/reader036/viewer/2022062722/56813aec550346895da35878/html5/thumbnails/12.jpg)
ต�วอย&าง 11.3 ฟั4งก�ช้�นท��ท$าการคื$านวณบนต�วช้�)
int strlen(s)char *s;{
char *p = s;while(*p != ‘\0’)
p++;return (p-s);
}
![Page 13: บทที่ 11 การเขียนโปรแกรมโดยใช้ข้อมูลชนิดพอยท์เตอร์](https://reader036.fdocuments.net/reader036/viewer/2022062722/56813aec550346895da35878/html5/thumbnails/13.jpg)
คื$าอธ์�บายฟั4งก�ช้�นต�วอย&าง 11.3
เป+นฟั4งก�ช้�นท��ใช้�คื$านวณหาคืวามยาวขีอง string โดย string ท��ต�องการหาคืวามยาวได�มาจากการ
ผ&านคื&าต$าแหน&งขีอง string มาทางรายการอาร�ก�วเมนต�ขีองฟั4งก�ช้�น
A B C D E F \0S
Pส�)นส6ดคืวามยาว = P-S
![Page 14: บทที่ 11 การเขียนโปรแกรมโดยใช้ข้อมูลชนิดพอยท์เตอร์](https://reader036.fdocuments.net/reader036/viewer/2022062722/56813aec550346895da35878/html5/thumbnails/14.jpg)
อาร�เรย�ขีองพอยท�เตอร�
ในภาษาซึ่� สามารถึท��จะม�อาร�เรย�ท��เป+นพอยท�เตอร�เช้&นเด�ยวก�บม�อาร�เรย�ท��เป+นขี�อม�ลช้น�ดอ��นๆได� เช้&น int *parray[10];
parray[0]
parray[1]
parray[2]
.
.
parray[9]
int.var
int.var
int.var
int.var
![Page 15: บทที่ 11 การเขียนโปรแกรมโดยใช้ข้อมูลชนิดพอยท์เตอร์](https://reader036.fdocuments.net/reader036/viewer/2022062722/56813aec550346895da35878/html5/thumbnails/15.jpg)
ต�วอย&าง 11.4 โปรแกรมอาร�เรย�ขีองพอยท�เตอร�
main(){
int j,n1=1;n2=2;n3=3;*ptr,*parray[4]; parray[1] = &n1; parray[2] = &n2; ptr = &n3; parray[3] = ptr;for(j=1;j<4;j++)
printf(“%d\n”,*parray[j]);}
![Page 16: บทที่ 11 การเขียนโปรแกรมโดยใช้ข้อมูลชนิดพอยท์เตอร์](https://reader036.fdocuments.net/reader036/viewer/2022062722/56813aec550346895da35878/html5/thumbnails/16.jpg)
ผลล�พธ์�ขีองโปรแกรมต�วอย&าง 11.4
เป+นโปรแกรมท��ใช้�อาร�เรย�เก#บคื&าต$าแหน&งขีอง int var
จะได�ผลล�พธ์�ด�งน�)1
2
3
![Page 17: บทที่ 11 การเขียนโปรแกรมโดยใช้ข้อมูลชนิดพอยท์เตอร์](https://reader036.fdocuments.net/reader036/viewer/2022062722/56813aec550346895da35878/html5/thumbnails/17.jpg)
คื$าอธ์�บายโปรแกรม
declaration
int n1=1,n2=2,n3=3,*ptr, *parray[4]
executable code
1 2 3parray
[0] [1] [2] [3] ptr
n1 n2 n3 1 2 3
n1 n2 n3
parray
[0] [1] [2] [3] ptr
![Page 18: บทที่ 11 การเขียนโปรแกรมโดยใช้ข้อมูลชนิดพอยท์เตอร์](https://reader036.fdocuments.net/reader036/viewer/2022062722/56813aec550346895da35878/html5/thumbnails/18.jpg)
อาร�เรย�ขีองพอยท�เตอร�บนต�วอ�กษร
เราสามารถึใช้�อาร�เรย�ขีองพอยท�เตอร�บนต�วอ�กษร ซึ่!�งก$าหนดคื&าเร��มต�นไปย�ง string ท��ม�คืวามยาวต&างก�นได� และแต&ละต�ว string ใช้�เน�)อท��ตามท��ต�องการเท&าน�)น
ซึ่!�งจะแสดงได�ด�งโปรแกรมต�วอย&างถึ�ดไปซึ่!�งจะร�บคื&าหมายเลขีเด�อน แล�วแสดงช้��อเด�อนน�)นทางหน�าจอ
![Page 19: บทที่ 11 การเขียนโปรแกรมโดยใช้ข้อมูลชนิดพอยท์เตอร์](https://reader036.fdocuments.net/reader036/viewer/2022062722/56813aec550346895da35878/html5/thumbnails/19.jpg)
ต�วอย&าง 11.5 อาร�เรย�ขีองพอยท�เตอร�บนต�วอ�กษร
char *month();
main()
{ int num;
puts(“Enter number of month”);
scanf(“%d”,num);
printf(“The name of month is %s”,month(num));
}
Char *month(n)Int n;{ static char *name []= { “illegal month”,“January”,
“February”,“March”,“April”,“May”,“June”,“July”, “August”,“September”, “October”,“November”, “December”};return((n<1 || n>12)?name[0]: name[n];)
}}
![Page 20: บทที่ 11 การเขียนโปรแกรมโดยใช้ข้อมูลชนิดพอยท์เตอร์](https://reader036.fdocuments.net/reader036/viewer/2022062722/56813aec550346895da35878/html5/thumbnails/20.jpg)
ผลล�พธ์�ขีองโปรแกรมต�วอย&าง 11.5
ผลลพธ์ Enter number of month
> 6
The name of month is June
![Page 21: บทที่ 11 การเขียนโปรแกรมโดยใช้ข้อมูลชนิดพอยท์เตอร์](https://reader036.fdocuments.net/reader036/viewer/2022062722/56813aec550346895da35878/html5/thumbnails/21.jpg)
คื$าอธ์�บายโปรแกรม
name[0]
name[1]
name[2]
name[12]
illegal month
January
February
.
.
December