1 / 21

บทที่ 11 การเขียนโปรแกรมโดยใช้ข้อมูลชนิดพอยท์เตอร์

บทที่ 11 การเขียนโปรแกรมโดยใช้ข้อมูลชนิดพอยท์เตอร์. พอยท์เตอร์ (Pointer). พอยท์เตอร์ คือ ตัวแปรซึ่งเก็บตำแหน่งของอีกตัวแปรหนึ่ง ซึ่งจะมีขนาดตามตำแหน่ง machine และพอยท์เตอร์ต้องชี้ไปยัง type ของข้อมูลที่แน่ชัด เป็นการเข้าถึงข้อมูลแบบ indirectly รูปแบบการประกาศ. type *var;.

shima
Download Presentation

บทที่ 11 การเขียนโปรแกรมโดยใช้ข้อมูลชนิดพอยท์เตอร์

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. บทที่ 11การเขียนโปรแกรมโดยใช้ข้อมูลชนิดพอยท์เตอร์

  2. พอยท์เตอร์ (Pointer) พอยท์เตอร์ คือ ตัวแปรซึ่งเก็บตำแหน่งของอีกตัวแปรหนึ่ง ซึ่งจะมีขนาดตามตำแหน่ง machineและพอยท์เตอร์ต้องชี้ไปยังtypeของข้อมูลที่แน่ชัด เป็นการเข้าถึงข้อมูลแบบ indirectly • รูปแบบการประกาศ type *var;

  3. พอยท์เตอร์ (Pointer) (ต่อ) เช่น int *iptr;//เก็บตำแหน่งของตัวแปรอื่นที่เป็นinteger char *cptr //เก็บตำแหน่งของตัวแปรอื่นที่เป็น character iptr integer var cptr char var

  4. ตัวดำเนินการ ในภาษาซี ได้มีการกำหนดตัวดำเนินการ 2 ชนิดที่เกี่ยวกับพอยท์เตอร์ ดังนี้ • * หมายถึง indirect operator เป็นการประกาศใช้ตัวแปรพอยท์เตอร์นั้นละยังสามารถใช้เพื่อเข้าถึงเนื้อที่ของหน่วยความจำที่ตัวแปรนั้นชี้ • & หมายถึง address operator เป็นตัวดำเนินการที่ใช้เพื่อบอกตำแหน่งของตัวแปร แทนที่จะเป็นค่าที่เก็บข้อมูล

  5. ตัวดำเนินการ (ต่อ) ตัวอย่าง เช่น char *ptr = NULL; เป็น declaration :ทำการจองเนื้อที่ให้ตัวแปรพอยท์เตอร์ชื่อ ptrและชี้ไปยัง NULL *ptr = NULL; เป็น executable code :การให้ค่า NULLแก่หน่วยความจำตำแหน่งที่ ptrชี้ไป

  6. ตัวอย่าง 11.1 โปรแกรมที่ใช้พอยท์เตอร์ main() { int num = 3; int *ptr; ptr =# printf(“The value of num is %d. Its address is %lu.”,*ptr,(long)ptr); }

  7. ผลลัพธ์ของตัวอย่าง 11.1 • ผลลัพธ์ >The value of num is 3. Its address is 448925078. • หมายเหตุ ค่าตำแหน่งของตัวแปรไม่แน่นอนในแต่ละครั้งของการสั่งดำเนินการ

  8. คำอธิบายโปรแกรม

  9. ตัวอย่าง 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); } }

  10. ผลลัพธ์โปรแกรมตัวอย่าง 11.2 ผลลัพธ์ 1 2 3 4 5 6 หมายเหตุ ถ้าเราประกาศ int *ptr;เราสามารถให้ ptrชี้ไปยังอาร์เรย์ได้ดังนี้ptr = number; หรือ ptr = &number[0];

  11. อธิบายโปรแกรม11.2 number i = 0; i = 1; i = 2; i = 3; i = 4; i = 5; ptr 0 1 2 3 4 5 ptr ptr ptr ptr ptr

  12. ตัวอย่าง 11.3 ฟังก์ชันที่ทำการคำนวณบนตัวชี้ int strlen(s) char *s; { char *p = s; while(*p != ‘\0’) p++; return (p-s); }

  13. คำอธิบายฟังก์ชันตัวอย่าง11.3 • เป็นฟังก์ชันที่ใช้คำนวณหาความยาวของstring • โดยstringที่ต้องการหาความยาวได้มาจากการผ่านค่าตำแหน่งของ stringมาทางรายการอาร์กิวเมนต์ของฟังก์ชัน S P สิ้นสุดความยาว= P-S

  14. อาร์เรย์ของพอยท์เตอร์อาร์เรย์ของพอยท์เตอร์ ในภาษาซี สามารถที่จะมีอาร์เรย์ที่เป็นพอยท์เตอร์เช่นเดียวกับมีอาร์เรย์ที่เป็นข้อมูลชนิดอื่นๆได้ เช่น int *parray[10];

  15. ตัวอย่าง 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]); }

  16. ผลลัพธ์ของโปรแกรมตัวอย่าง11.4ผลลัพธ์ของโปรแกรมตัวอย่าง11.4 • เป็นโปรแกรมที่ใช้อาร์เรย์เก็บค่าตำแหน่งของ int var • จะได้ผลลัพธ์ดังนี้ 1 2 3

  17. declaration int n1=1,n2=2,n3=3,*ptr, *parray[4] executable code คำอธิบายโปรแกรม n1 n2 n3 n1 n2 n3 parray parray [0] [1] [2] [3] ptr [0] [1] [2] [3] ptr

  18. อาร์เรย์ของพอยท์เตอร์บนตัวอักษรอาร์เรย์ของพอยท์เตอร์บนตัวอักษร • เราสามารถใช้อาร์เรย์ของพอยท์เตอร์บนตัวอักษร ซึ่งกำหนดค่าเริ่มต้นไปยัง stringที่มีความยาวต่างกันได้ และแต่ละตัว stringใช้เนื้อที่ตามที่ต้องการเท่านั้น • ซึ่งจะแสดงได้ดังโปรแกรมตัวอย่างถัดไปซึ่งจะรับค่าหมายเลขเดือน แล้วแสดงชื่อเดือนนั้นทางหน้าจอ

  19. 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];) } } ตัวอย่าง 11.5อาร์เรย์ของพอยท์เตอร์บนตัวอักษร

  20. ผลลัพธ์ของโปรแกรมตัวอย่าง 11.5 ผลลัพธ์ Enter number of month > 6 The name of month is June

  21. คำอธิบายโปรแกรม

More Related