1 / 37

ตัวชี้หรือพอยน์เตอร์ (Pointer)

ตัวชี้หรือพอยน์เตอร์ (Pointer). ตัวชี้ (Pointer).

anoki
Download Presentation

ตัวชี้หรือพอยน์เตอร์ (Pointer)

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. ตัวชี้หรือพอยน์เตอร์ (Pointer) created by Dararat Saelee , Computer Sciences Department, Faculty of Science, PSU-HatYai

  2. ตัวชี้ (Pointer) เป็นข้อมูลชนิดหนึ่งที่สามารถสร้างขึ้นในหน่วยความจำหลักเมื่อมีการเรียกใช้เท่านั้น ไม่มีการจองเนื้อที่ไว้ล่วงหน้าเหมือนกับการประกาศตัวแปรที่ผ่านมา โดยเมื่อมีการเรียกใช้ข้อมูลแบบพอยน์เตอร์ ระบบจะจองเนื้อที่ว่างในหน่วยความจำให้ หลังจากทำงานเสร็จแล้วก็ต้องมีการคืนเนื้อที่หน่วยความจำนั้นให้แก่ระบบเพื่อให้โปรแกรมส่วนอื่นสามารถใช้งานเนื้อที่นั้นได้ต่อไป created by Dararat Saelee , Computer Sciences Department, Faculty of Science, PSU-HatYai

  3. Pointer (ต่อ) ตัวแปรแบบพอยน์เตอร์ ทำหน้าที่เก็บตำแหน่งที่อยู่ (address)ของตัวแปรหรือข้อมูลที่มีในหน่วยความจำ นั่นคือตัวมันเองไม่ได้เก็บข้อมูล เพียงแต่ชี้ไปยังที่เก็บข้อมูลอีกทีหนึ่ง เพื่อทำให้การทำงานของโปรแกรมมีประสิทธิภาพมากขึ้น created by Dararat Saelee , Computer Sciences Department, Faculty of Science, PSU-HatYai

  4. Pointer (ต่อ) ตัวอย่างเช่น score เป็นตัวแปรเก็บค่าจำนวนเต็ม 120 int score; score = 120; score 120 created by Dararat Saelee , Computer Sciences Department, Faculty of Science, PSU-HatYai

  5. address ptr 784 784 120 ptr หรือ 120 Pointer (ต่อ) ในขณะที่ถ้ากำหนด ptr เป็นตัวแปรพอยน์เตอร์ที่ชี้ไปยังข้อมูล เลขจำนวนเต็ม 120 int *ptr; *ptr = 120; เป็นการกำหนดให้เนื้อที่ที่ ตัวแปร ptr ชี้ไปมีค่าเท่ากับ 120 created by Dararat Saelee , Computer Sciences Department, Faculty of Science, PSU-HatYai

  6. การประกาศตัวแปรพอยน์เตอร์การประกาศตัวแปรพอยน์เตอร์ รูปแบบtype *variable_name เมื่อ type = ชนิดของตัวแปร เช่น int, float * = เป็นเครื่องหมายที่กำหนดว่าเป็นตัวแปรแบบ พอยน์เตอร์ variable_name = ชื่อตัวแปร Exampleint *ptr; char *pt; float *ft; created by Dararat Saelee , Computer Sciences Department, Faculty of Science, PSU-HatYai

  7. 111 ptr 111 int 200 pt 200 char 484 ft 484 float การประกาศตัวแปรพอยน์เตอร์ (ต่อ) Exampleint *ptr; char *pt; float *ft; created by Dararat Saelee , Computer Sciences Department, Faculty of Science, PSU-HatYai

  8. เครื่องหมายที่ใช้กับพอยน์เตอร์เครื่องหมายที่ใช้กับพอยน์เตอร์ - เครื่องหมาย &(ampersand) ใช้สำหรับกำหนดค่าเริ่มต้นให้กับพอยน์เตอร์ โดยการนำเอาตำแหน่งที่อยู่ (Address) ของตัวแปรในหน่วยความจำมาใช้งาน - เครื่องหมาย * ใช้สำหรับนำเอาข้อมูลในตำแหน่งที่ตัวแปรพอยน์เตอร์นั้นชี้อยู่ ออกมาใช้งาน created by Dararat Saelee , Computer Sciences Department, Faculty of Science, PSU-HatYai

  9. score 888 เครื่องหมายที่ใช้กับพอยน์เตอร์ (ต่อ) ตัวอย่าง score เป็นตัวแปรเก็บค่าจำนวนเต็ม 120 int score; score = 120; printf(”&score =%d”,&score); 120 &score = 888 created by Dararat Saelee , Computer Sciences Department, Faculty of Science, PSU-HatYai

  10. 111 ptr 111 52 200 pt 200 ‘G’ เครื่องหมายที่ใช้กับพอยน์เตอร์ (ต่อ) Exampleint *ptr; จากรูปptr มีค่าเท่ากับ 111 *ptr มีค่าเท่ากับ 52 Examplechar *pt; จากรูปpt มีค่าเท่ากับ 200 *pt มีค่าเท่ากับ ‘G’ created by Dararat Saelee , Computer Sciences Department, Faculty of Science, PSU-HatYai

  11. สัญลักษณ์และความหมาย int *pt ประกาศตัวแปร pt เป็นพอยน์เตอร์ชนิด integer &a address operator ของ a pt = &a; ตัวแปร pt เก็บค่า address operator ของ a *pt ค่าตรงตำแหน่งที่ pt ชี้อยู่ b = *pt กำหนดค่าตัวแปร b เท่ากับค่าที่ pt ชี้อยู่ created by Dararat Saelee , Computer Sciences Department, Faculty of Science, PSU-HatYai

  12. ผลการทำงาน a = 10 b = 0100 &b = 0370 เครื่องหมายที่ใช้กับพอยน์เตอร์ (ต่อ) Example int a, *b; a = 10; b = &a; printf (“a=%d”, *b); a b 10 0100 0370 0100 10 created by Dararat Saelee , Computer Sciences Department, Faculty of Science, PSU-HatYai

  13. ผลการทำงาน a = 10 b = 0370 &b = ??? เครื่องหมายที่ใช้กับพอยน์เตอร์ (ต่อ) a b 0100 0370 Example int a, *b; *b = 10; a = *b; printf (“a=%d”, a); *b 10 10 Demo Program created by Dararat Saelee , Computer Sciences Department, Faculty of Science, PSU-HatYai

  14. คณิตศาสตร์ของพอยน์เตอร์คณิตศาสตร์ของพอยน์เตอร์ ตัวแปรพอยน์เตอร์สามารถใช้เครื่องหมายทางคณิตศาสตร์มากระทำได้เช่นเดียวกับตัวแปรทั่วๆไป แต่ไม่สามารถใช้ได้ทั้งหมด เครื่องหมายทางคณิตศาสตร์ที่ใช้ได้ คือ 1. เครื่องหมาย +คือ การบวก 2. เครื่องหมาย - คือ การลบ 3. เครื่องหมาย + +คือ การเพิ่มค่าครั้งละ 1 4. เครื่องหมาย- -คือ การลดค่าครั้งละ 1 created by Dararat Saelee , Computer Sciences Department, Faculty of Science, PSU-HatYai

  15. คณิตศาสตร์ของพอยน์เตอร์คณิตศาสตร์ของพอยน์เตอร์ Example ถ้ากำหนด int*p; เป็นตัวแปร pointer ชี้ไปยังที่เก็บ integer p++; p--; เพิ่มหรือลดทีละ 4 byte address p = p+2; เพิ่มค่า p อีก 8 (4x2)byte address p = p-5; ลดค่า p ลง 20 (4x5) byte address Demo Program created by Dararat Saelee , Computer Sciences Department, Faculty of Science, PSU-HatYai

  16. คณิตศาสตร์ของพอยน์เตอร์คณิตศาสตร์ของพอยน์เตอร์ Example ถ้ากำหนด int*p; เป็นตัวแปร pointer ชี้ไปยังที่เก็บ integer x = *p++; or x = *(p++); กำหนด x = ค่าที่ชี้โดย p แล้ว เพิ่มค่า p อีก 4 y = (*p)++; เพิ่มค่าที่ชี้โดย p อีก 1 created by Dararat Saelee , Computer Sciences Department, Faculty of Science, PSU-HatYai

  17. การส่งพารามิเตอร์ด้วยตำแหน่ง(passing by address) การกำหนดฟังก์ชัน • type function-name (formal parameters) • formal parameters : pointer variables • Ex. int func1 (int *a, int *b) { return *a + *b; } created by Dararat Saelee , Computer Sciences Department, Faculty of Science, PSU-HatYai

  18. การส่งพารามิเตอร์ด้วยตำแหน่ง(passing by address) การเรียกใช้ฟังก์ชัน • var = function-name (arguments) • arguments : address of variables • Ex. z = func1 (&x, &y); ส่งค่า address ของ x และ y created by Dararat Saelee , Computer Sciences Department, Faculty of Science, PSU-HatYai

  19. การส่งพารามิเตอร์ด้วยตำแหน่ง(passing by address) int func1 (int *a, int *b) { return *a + *b; } void main() { int x,y,z; z = func1 (&x, &y); } name value address 12 x y 5 7 00140 00142 *(00140)+*(00142) 5+7 = 12 00140 , 00142 created by Dararat Saelee , Computer Sciences Department, Faculty of Science, PSU-HatYai

  20. การส่งพารามิเตอร์ด้วยตำแหน่งการส่งพารามิเตอร์ด้วยตำแหน่ง การเรียกใช้ฟังชัน การกำหนดฟังชัน Demo program created by Dararat Saelee , Computer Sciences Department, Faculty of Science, PSU-HatYai

  21. ประโยชน์ของการส่งพารามิเตอร์ด้วยตำแหน่งประโยชน์ของการส่งพารามิเตอร์ด้วยตำแหน่ง • เมื่อต้องมีการเปลี่ยนแปลงค่าพารามิเตอร์หลังจากการทำงานของฟังก์ชัน • เมื่อต้องการส่งค่ากลับจากฟังก์ชันมากกว่า 1 ค่า created by Dararat Saelee , Computer Sciences Department, Faculty of Science, PSU-HatYai

  22. Class Exercise • เขียนโปรแกรมเพื่ออ่านค่าส่วนสูงของนศ.จากแฟ้มข้อมูล แล้วเรียกใช้ฟังก์ชัน 1 ฟังก์ชันที่คำนวณหาค่าส่วนสูงเฉลี่ยและค่าส่วนสูงของคนที่สูงที่สุดและแสดงผลทั้ง 2 ค่าที่โปรแกรมหลัก • ปรับโปรแกรมข้างต้นให้รับค่าน้ำหนักและใช้ฟังก์ชันเดียวกันทำงานให้เช่นกัน created by Dararat Saelee , Computer Sciences Department, Faculty of Science, PSU-HatYai

  23. พอยน์เตอร์และอาร์เรย์พอยน์เตอร์และอาร์เรย์ พอยน์เตอร์และอาร์เรย์มีความสัมพันธ์กันอย่างมาก สามารถใช้งานแทนกันได้ ซึ่งการอ้างข้อมูลในอาร์เรย์ สามารถอ้างโดยชื่อตัวแปรอาร์เรย์ หรือ ใช้ตัวแปรพอยน์เตอร์เพื่อแสดงตำแหน่งของตัวแปรอาร์เรย์ก็ได้ ข้อดีของพอยน์เตอร์ คือ การใช้งานเร็วกว่า แลไม่เปลืองเนื้อที่ แต่พอยน์เตอร์จะเข้าใจยากกว่า created by Dararat Saelee , Computer Sciences Department, Faculty of Science, PSU-HatYai

  24. พอยน์เตอร์และอาร์เรย์ (ต่อ) Example int x[4] = {1,2,3,4}; int *px[4]; px[0] = &x[0]; px[1] = &x[1]; px[2] = &x[2]; px[3] = &x[3]; created by Dararat Saelee , Computer Sciences Department, Faculty of Science, PSU-HatYai

  25. px[0] px[1] px[2] px[3] 03EE 03F0 03F2 03F4 พอยน์เตอร์และอาร์เรย์ (ต่อ) int x[4] = {1, 2, 3, 4};int *px[4]; px[0] px[1] px[2] px[3] x[0] x[1] x[2] x[3] 1 2 3 4 03EE 03F0 03F2 03F4 0210 0212 0214 0216 px[0] = &x[0]; px[1] = &x[1]; px[2] = &x[2]; px[3] = &x[3]; 0210 0212 0214 0216 created by Dararat Saelee , Computer Sciences Department, Faculty of Science, PSU-HatYai

  26. px[0] px[1] px[2] px[3] 03EE 03F0 03F2 03F4 พอยน์เตอร์และอาร์เรย์ (ต่อ) int x[4] = {1, 2, 3, 4};int *px[4]; px[0] px[1] px[2] px[3] x[0] x[1] x[2] x[3] 1 2 3 4 03EE 03F0 03F2 03F4 0210 0212 0214 0216 *px[0] = x[0]; *px[1] = x[1]; *px[2] = x[2]; *px[3] = x[3]; 0210 0212 0214 0216 created by Dararat Saelee , Computer Sciences Department, Faculty of Science, PSU-HatYai

  27. Demo program created by Dararat Saelee , Computer Sciences Department, Faculty of Science, PSU-HatYai

  28. พอยน์เตอร์และสตริง สตริงเป็น Array ที่เก็บตัวอักษรเรียงต่อๆกันไป และปิดท้ายด้วย “\0” เพื่อแสดงการสิ้นสุดของข้อความ ดังนั้นจึงสามารถประกาศตัวแปรเป็น Pointer เพื่อเก็บข้อความในลักษณะเดียวกับ Array ได้เช่นกัน รูปแบบ char *ch; โดยที่สามารถใช้งานได้เหมือนกับ Array ทั่วไป created by Dararat Saelee , Computer Sciences Department, Faculty of Science, PSU-HatYai

  29. พอยน์เตอร์และสตริง (ต่อ) สำหรับตัวแปรชนิดข้อความ โดยปกติจะใช้ตัวแปรอาร์เรย์ชนิดอักขระสร้างขึ้นมาให้มีจำนวนอักขระทั้งหมดในข้อความและบวกเพิ่มอีก 1 สำหรับ ‘\0’ (อักขระNULL) เพื่อบอกให้รู้ว่าเป็นข้อความ Example char name[8] = “Jaikaew”; char *pt_name; pt_name = name; printf (“Address of name = %p\n”,pt_name); printf (“name = %s\n”,name); created by Dararat Saelee , Computer Sciences Department, Faculty of Science, PSU-HatYai

  30. พอยน์เตอร์และตัวแปรชนิดโครงสร้างพอยน์เตอร์และตัวแปรชนิดโครงสร้าง เราสามารถใช้ตัวแปรพอยน์เตอร์มาชี้ตำแหน่งของข้อมูลหรือตัวแปรชนิดโครงสร้างได้เช่นกัน Examplestruct infor { char name[15]; float balance; }; struct infor *p; created by Dararat Saelee , Computer Sciences Department, Faculty of Science, PSU-HatYai

  31. พอยน์เตอร์และตัวแปรชนิดโครงสร้าง (ต่อ) ในการอ้างถึงตัวแปร balance ภายในโครงสร้างชื่อ infor ได้โดยผ่านตัวแปรพอยน์เตอร์ดังนี้ (*p).balance หรือ pbalance created by Dararat Saelee , Computer Sciences Department, Faculty of Science, PSU-HatYai

  32. การจัดสรรหน่วยความจำแบบไดนามิกการจัดสรรหน่วยความจำแบบไดนามิก ความหมาย Dynamic Allocationเป็นการจองพื้นที่แบบชั่วคราวสำหรับเก็บค่าของตัวแปร โดยเมื่อรันโปรแกรมตัวแปรจะยังไม่ถูกสร้างขึ้น ดังนั้นพื้นที่ในหน่วยความจำจะไม่ถูกจองไว้ก่อน เมื่อต้องการใช้งานงานแปร จึงทำการจองพื้นที่ สำหรับเก็บค่าของตัวแปรนั้นขึ้นมา และเมื่อใช้งานเสร็จก็สามารถคืนหน่วยความจำเพื่อใช้งานอย่างอื่น created by Dararat Saelee , Computer Sciences Department, Faculty of Science, PSU-HatYai

  33. การจัดสรรพื้นที่ด้วยฟังก์ชัน malloc() รูปแบบ *pt = malloc (size) โดยที่ *ptตัวแปรพอยน์เตอร์เพื่อใช้รับค่าที่ได้กลับฟังก์ชัน malloc() ซึ่งเป็นค่าตำแหน่งแรกของพื้นที่ทั้งหมดที่จองไว้ ในกรณีที่ไม่สามารถจองพื้นที่ได้ ฟังก์ชันจะส่งค่า NULLมาให้แทน sizeขนาดของพื้นที่ในหน่วยความจำ ที่ต้องการจอง มีหน่วยเป็นไบต์ created by Dararat Saelee , Computer Sciences Department, Faculty of Science, PSU-HatYai

  34. การจัดสรรพื้นที่ด้วยฟังก์ชัน malloc() Example int *pt; pt = malloc(8); Example float *pt; pt = malloc(2*sizeof (float)); created by Dararat Saelee , Computer Sciences Department, Faculty of Science, PSU-HatYai

  35. การคืนพื้นที่ด้วยฟังก์ชัน free() รูปแบบ free(pt); โดยที่ pt ตัวแปรพอยน์เตอร์ ซึ่งเป็นตัวชี้ตำแหน่งในหน่วยความจำที่เราต้องการคืน created by Dararat Saelee , Computer Sciences Department, Faculty of Science, PSU-HatYai

  36. ตัวอย่างโปรแกรม #include<stdio.h> pt = malloc(length+1*sizeof(char)); #include<stdlib.h> strcpy (pt,str); #include<string.h> clrscr (); main () printf (“%s”,pt); { char str[ ] = “Infopress Developer free (pt); Book”; } char *pt; int length; length = strlen (str); ผลการรัน Infopress DeveloperBook created by Dararat Saelee , Computer Sciences Department, Faculty of Science, PSU-HatYai

  37. Motto Today ทำดีต่อคนทั้งโลกแต่ไม่เคยทำดีกับพ่อแม่ ความดีทั้งหลายนั้นก็สูญเปล่า created by Dararat Saelee , Computer Sciences Department, Faculty of Science, PSU-HatYai

More Related