1 / 23

Pointer

Pointer. ความหมายของ พอยเตอร์ การใช้งาน พอยเตอร์ พอยเตอร์ กับอาร์เรย์ และ พอยเตอร์ กับ สตรัคเจอร์. int number = 2;. 1. ชื่อ อ้างอิง. number. number. 2. 2. 3. ค่า. AA05. AA04. AA02. AA02. 2. address. องค์ประกอบของหน่วยความจำ. 1. อ้างอิงค่าที่เก็บใน address รูปแบบ ชื่อตัวแปร

xanto
Télécharger la présentation

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 ความหมายของพอยเตอร์การใช้งานพอยเตอร์พอยเตอร์กับอาร์เรย์ และพอยเตอร์กับสตรัคเจอร์

  2. int number = 2; 1. ชื่ออ้างอิง number number 2 2 3.ค่า AA05 AA04 AA02 AA02 2. address องค์ประกอบของหน่วยความจำ

  3. 1. อ้างอิงค่าที่เก็บใน address รูปแบบ ชื่อตัวแปร เช่น numberจะหมายถึงค่า 2 2. อ้างอิงค่า address รูปแบบ &ชื่อตัวแปร เช่น &number จะหมายถึง AA02 number 2 AA02 การอ้างอิงค่าจากตัวแปร

  4. 1. ตัวแปรพื้นฐาน (เก็บค่าตามชนิดข้อมูล) รูปแบบ ชนิดข้อมูล ชื่อตัวแปร; เช่น float a = 3.14; 2. ตัวแปรพอยเตอร์(เก็บค่า address)รูปแบบ ชนิดข้อมูล *ชื่อตัวแปร;เช่น float *pA = &a; หมายเหตุ ชนิดข้อมูลของตัวแปรพอยเตอร์ต้องตรงกับ ชนิดข้อมูลที่จะทำการเก็บ address a 3.14 AA00 pA AA00 ตัวแปรเชิงเดียว xxxx

  5. ตัวแปรพอยเตอร์เป็นตัวแปรเก็บค่าตำแหน่งแต่การเก็บค่า address ให้เสมือนว่าทำการชี้ไปที่กล่อง adressนั้น a 3.14 float a = 3.14; AA00 pA float *pA = &a; ขยายความ ความหมายพอยเตอร์ xxxx

  6. ชนิดข้อมูลของพอยเตอร์ ไม่ตรงกับชนิดข้อมูลที่จะทำการชี้ char x = ‘a’; ผิด compile error int *pX = &x; char x = ‘a’; ชนิดข้อมูลต้อง Match กัน สิ่งที่ทำไม่ได้ char *pX = &x;

  7. x หลังจากทำการชี้ด้วยคำสั่งต่อไปนี้แล้ว… ‘a’ char x = ‘a’; char pX = &x; AA22 pX การอ้างอิง ข้อมูลในaddressรูปแบบ *ชื่อตัวแปรพอยเตอร์เช่น *pXจะหมายถึง ‘a’(เหมือนอ้างอิง x) xxxx การอ้างอิง ค่า addressรูปแบบ ชื่อตัวแปรพอยเตอร์เช่น pXจะหมายถึง AA22 การอ้างอิงค่าหลังจากทำการชี้แล้ว

  8. a pA #include <stdio.h> main(){int a = 2;int *pA = &a;printf(“a = %d”, a);printf(“*pA = %d”, *pA);printf(“pA(hex) = %p”, pA);printf(“pA(dec) = %d”, pA);} 2 AA10 xxxx %p คือรหัสแสดง address เป็นเลขฐาน 16%d หรือ %u คือรหัสแสดง addressเป็นเลขฐาน 10 a = 2*pA = 2pA(hex) = AA10pA(dec) = 43536 ตัวอย่าง Code เพิ่มความเข้าใจ

  9. นำพอยเตอร์มากำหนดค่าก่อนทำการชี้นำพอยเตอร์มากำหนดค่าก่อนทำการชี้ int *pt; *pt = 2; ผิด compile error pt pointer ยังไม่ได้ชี้ไปที่ addressใด ???? xxxx สิ่งที่ทำไม่ได้เกี่ยวกับการอ้างอิง pointer

  10. int a[3] = {1,2,3}; การให้พอยเตอร์ชี้ไปที่ indexที่ 0 (ทำได้ 2 กรณี)pA = a; // อ้างอิงชื่อโดยตรงหรือpA = &a[0];// อ้างอิงโดยใช้ & int *pA; การให้พอยเตอร์ชี้ไปที่ indexที่อื่นๆpA = &a[index]; พอยเตอร์กับอาร์เรย์

  11. int a[3] = {1,2,3}; int *pA= a; pA B20 B22 B24 ชื่ออ้างอิงค่าใน array(3 แบบ) 1 2 3 xxxx a[0] a[1] a[2] pA[0] pA[1] pA[2] *pA *(pA+1) *(pA+2) สิ่งที่ได้ตามมาหลังทำการชี้ไปที่ข้อมูลอาร์เรย์

  12. int a[4] = {1,2,3,4}; int *pA= &a[1]; pA B20 B22 B24 B26 1 2 3 4 xxxx a[3] a[0] a[1] a[2] pA[2] pA[0] pA[1] pA[-1] index ของพอยเตอร์ตัวที่ชี้ไป จะเริ่มจาก 0 กรณีที่ 2 สิ่งที่ได้หลังการอ้างอิง *(pA-1) *pA *(pA+1) *(pA+2)

  13. ทบทวนการอ้างอิงค่าในพอยเตอร์int *pt;- ptจะหมายถึงค่า address- *ptจะหมายถึง ข้อมูลใน address นั้น *pt+nความหมายคือ เอาค่าข้อมูลใน address มาบวก n *(pt+n) ความหมายคือ อ้างค่าข้อมูลใน address + n คณิตศาสตร์เกี่ยวกับพอยเตอร์ ใช้อ้างอิงในอาร์เรย์จากหัวข้อที่แล้ว

  14. Structure ความหมายของชนิดข้อมูลแบบโครงสร้างการสร้างสตรัคเจอร์พอยเตอร์กับสตรัคเจอร์

  15. สตรัคเจอร์คือ ชนิดข้อมูลที่รวบรวมชนิดข้อมูลพื้นฐานมาสร้างเป็นกลุ่มข้อมูลใหม่ (โดยชนิดข้อมูลไม่ต้องเหมือนกันก็ได้) ข้อจำกัดอาร์เรย์คือ ชนิดข้อมูลต้องเหมือนกันทั้งชุด struct Student{ char firstName[10]; char lastName[10];intid; float grade;}; สตรัคเจอร์ (ชนิดข้อมูลแบบโครงสร้าง)

  16. structชื่อโครงสร้างหลัก{สมาชิก 1;สมาชิก 2;สมาชิก 3;structชื่อโครงสร้างย่อย {สมาชิก 1; สมาชิก 2; สมาชิก 3; };}; structชื่อโครงสร้าง{สมาชิก 1;สมาชิก 2;สมาชิก 3; …. ….}; structure structure ซ้อน structure รูปแบบการสร้าง Structure

  17. ต้องสร้างตัวแปรของ structure ขึ้นมา (ทำได้ 2 กรณี) ประกาศแบบที่ 1 ประกาศแบบที่ 2 structชื่อโครงสร้าง{สมาชิก 1;สมาชิก 2;สมาชิก 3; …. ….}ชื่อตัวแปร; structชื่อโครงสร้าง{สมาชิก 1;สมาชิก 2;สมาชิก 3; …. ….};structชื่อโครงสร้าง ชื่อตัวแปร; การนำ structure มาใช้งาน ชื่อตัวแปร สามารถเป็นอาร์เรย์ หรือพอยเตอร์ก็ได้

  18. std1 firstName lastName struct Student{ char firstName[10]; char lastName[10];int id; float grade;}std1; id grade ตัวอย่างโครงสร้างข้อมูล

  19. std1 struct Student{ char Name[10];int id; float grade;}std1 = {“oak”, 1359, 4.00}; firstName “oak” id 1359 หรือ grade 4.00 struct Student{ char Name[10];int id; float grade;};struct Student std1 = {“oak”, 1359, 4.00}; การกำหนดค่า เริ่มต้น ให้กับ structure ต้องใส่ชนิดข้อมูลให้ตรงตามลำดับที่ประกาศไว้std1 = {1359,”oak”, 4.00}; ผิด

  20. การอ้างอิงสมาชิกทำได้โดยอ้างอิงผ่าน . (dot)รูปแบบ ชื่อตัวแปรโครงสร้าง.สมาชิกหรือกรณีเป็น structure ซ้อน structureรูปแบบ ตัวแปรโครงสร้างหลัก.ตัวแปรโครงสร้างใน.สมาชิกใน struct Student{ char Name[10];int id; float grade;};struct Student std1; std1.Name std1.id std1.grade การอ้างอิงตัวแปรสมาชิกของ structure

  21. std1 struct Student{ char Name[10];int id; float grade;}std1;strcpy(std1.Name, “oak”);std1.id = 1359;std1.grade = 4.00; firstName “oak” id 1359 grade 4.00 การกำหนดค่าให้สมาชิกโครงสร้างแบบ ข้อความ จะกำหนดผ่านฟังก์ชั่น strcpy()รูปแบบ strcpy(ตัวแปร, ข้อความ)หมายเหตุ ต้องทำการ include string.hเข้ามาในโปรแกรมด้วย การกำหนดค่าให้ Structure

  22. std1 struct Student{ char Name[10];int id; float grade;}struct Student std1;struct Student *pStd1;pStd1 = &std1; firstName id grade pStd1 พอยเตอร์กับสตรัคเจอร์ ตัวแปรพอยเตอร์ชี้ไปที่ address ของตัวแปรโครงสร้าง

  23. หลังจากทำการชี้ไปที่ตัวแปรโครงสร้างแล้วหลังจากทำการชี้ไปที่ตัวแปรโครงสร้างแล้ว struct Student{ char Name[10];int id; float grade;}struct Student std1;struct Student *pStd1;pStd1 = &std1; สิ่งที่สามารถอ้างอิงหลังทำการชี้ pStd1->Name เหมือน std1.NamepStd1->id เหมือน std1.idpStd1->grade เหมือน std1.gradeรูปแบบชื่อตัวแปรพอยเตอร์->สมาชิก การอ้างอิงค่าสมาชิกโดยใช้พอยเตอร์

More Related