1 / 28

P O I N T E R

P O I N T E R. ตัวชี้. Created By Tasanawan Soonklang Template PPT By http://www.templateswise.com. เป็น ชื่อ ที่ใช้แทนข้อมูลหนึ่งๆ การประกาศตัวแปรเป็นการกำหนดชื่อเพื่อใช้แทนข้อมูล มีการจองเนื้อที่ในหน่วย ความจำเพื่อเก็บข้อมูล. a. 400 402 404. ตัวแปร. int a; a = 10;. 10. a. 400

jenaya
Télécharger la présentation

P O I N T E R

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. P O I N T E R ตัวชี้ Created By Tasanawan Soonklang Template PPT By http://www.templateswise.com

  2. เป็นชื่อที่ใช้แทนข้อมูลหนึ่งๆเป็นชื่อที่ใช้แทนข้อมูลหนึ่งๆ การประกาศตัวแปรเป็นการกำหนดชื่อเพื่อใช้แทนข้อมูล มีการจองเนื้อที่ในหน่วย ความจำเพื่อเก็บข้อมูล a 400 402 404 ตัวแปร int a; a = 10; 10

  3. a 400 402 404 int a; a = 10; 10 ptr 400 800 พอยเตอร์ หรือ ตัวชี้ • ตัวแปรพอยเตอร์(pointer)ทำหน้าที่เป็นตัวชี้ • เก็บแอดเดรส (address) ของข้อมูล • แอดเดรส คือ ตำแหน่งในหน่วยความจำ int *ptr; ptr = &a;

  4. ใช้เครื่องหมาย * ตัวอย่างเช่น int *p; char *pch; การเข้าถึงข้อมูล *p = 20; *pch = ‘x’; printf(“%d”,*p); printf(“%c”,*pch); รูปแบบการประกาศ ชนิดข้อมูล *ชื่อตัวแปรพอยเตอร์ ;

  5. ใช้เครื่องหมาย & คล้ายกับการใช้ scanf int a; scanf(“%d”,&a); 1 int age; 2 int *ptr; 3 age = 25; 4 ptr = &age; การกำหนดค่า ชื่อตัวแปรพอยเตอร์ = &ชื่อตัวแปรที่ต้องการชี้;

  6. ข้อแตกต่างระหว่างage และ&age age อ้างถึงค่าที่เก็บในตัวแปร &age อ้างถึงแอดเดรสของตัวแปร 1 int age; 2 int *ptr; 3 age = 25; 4 ptr = &age; 5 printf(“%d”,age); Value of age ptr age การใช้งาน Address of age 25 72009 72009

  7. ข้อแตกต่างระหว่างptr และ*ptr ptr อ้างถึงแอดเดรส *ptr อ้างถึงค่าที่เก็บในแอดเดรส 1 int age, ru=10; 2 int *ptr; 3 age = 25; 4 ptr = &age; 5 printf(“%d”,age); 6 *ptr = ru; Value of age ptr age การใช้งาน *ptr); Address of age 25 72009 10 72009

  8. 25 ptr ru age 1 int age, ru=10; 2 int *ptr; 3 age = 25; 4 ptr = &age; 5 printf(“%d”,*ptr); 6*ptr = ru; 7 printf(“%d”,*ptr); 25 10 72009 72009 72011 10 72011 1 int age, ru=10; 2 int *ptr; 3 age = 25; 4 ptr = &age; 5 printf(“%d”,*ptr); 6ptr = &ru; 7 printf(“%d”,*ptr); 10

  9. 100 #include<stdio.h> main() { int *p,q; q = 100; p = &q; printf("%d", *p); } #include<stdio.h> main() { int *p; float q, temp; temp = 1234.34; p = &temp; q = *p; printf("%f",q); } can’t convert float to int

  10. Pointer & Array • พอยเตอร์และอาร์เรย์มีความสัมพันธ์กัน • ชื่อตัวแปรอาร์เรย์จะเก็บแอดเดรสอยู่แล้ว • สามารถใช้พอยเตอร์ชี้แทนการระบุตำแหน่ง (index) ได้ • ทำงานได้รวดเร็วกว่า โดยเฉพาะเมื่อข้อมูลมีขนาดใหญ่ • พอยเตอร์จะชี้ไปยังตำแหน่งแรกของอาร์เรย์ • อ้างถึงสมาชิกตัวใดๆ ได้โดยการกระทำทางคณิตศาสตร์โดยใช้เครื่องหมาย ++, --, +, -

  11. 1 int x[5] = {1,2,3,4,5}; 2 int *ptr; 3 ptr = x; 4 ptr = &x[2]; x 400 402 404 400 ptr 800 404 ตัวอย่าง 1 2 3 X[2] ptr+1 อ้างถึงแอดเดรสของ x[1] ptr+2 อ้างถึงแอดเดรสของ x[2] *(ptr+2) อ้างถึงค่าของ x[2]

  12. 1 2 3 1 2 3 #include "stdio.h" int a[10] = {1,2,3,4,5,6,7,8,9,10}; main() { int *p; p = a; printf("%d %d %d\n",*p, *(p+1), *(p+2)); printf("%d %d %d",a[0],a[1],a[2]); } Pointers are fun #include "stdio.h" char str[ ] = "Pointers are fun"; main() { char *p; int i; p = str; for(i=0;p[i];i++) printf("%c",p[i]); }

  13. Enter a string: ThAiLaNd THAILAND thailand #include "stdio.h" #include "ctype.h" main() { char str[80] ; int i; printf("Enter a string :"); gets(str); for(i=0;str[i];i++) str[i] = toupper(str[i]); printf("%s\n",str); for(i=0;str[i];i++) str[i] = tolower(str[i]); printf("%s\n",str); } char *p; p = str; while(*p) { *p = toupper(*p); p++; } printf("%s\n",str); while(*p) *p++ = tolower(*p);

  14. Pointers are fun Nuf era sretnioP #include "stdio.h" #include "string.h" char str1[] = "Pointers are fun"; main() { char str2[80], *p1, *p2; p1 = str1+strlen(str1)-1; p2 = str2; while(p1>=str1) { *p2 = *p1; *p1--; *p2++; } printf("%s\n%s",str1,str2); } *p2++ = *p1--;

  15. #include <stdio.h> #define n 5 char days[ ][10] = {"monday","tuesday","wednesday", "thursday", "friday""}; void print_strings(char *table[ ], int num); main() { print_strings(days,n); } void print_strings(char *table[ ], int num) { int i; for (i = 0; i < num; i++) printf(“%d %s\n", table[i], table[i]); } 0000 monday 0007 tuesday 0015 wednesday 0025 thursday 0034 friday *days[ ] *(days + i) *(days+0) -> days[0] = “monday” *(days+1) -> days[1] = “tuesday” *(days+2) -> days[2] = “wednesday” *(days+3) -> days[3] = “thursday” *(days+4) -> days[4] = “friday”

  16. days[0] days[0]+0 *(days+0) days[0] days[1] days[2] days[3] days[4] days[4]+2 *(days+4)+2

  17. Enter string1: Computer science Enter string2: Information technology Swap strings String1 = Information technology String2 = Computer science Lab 10 • ใช้ index ในการอ้างถึงค่า • เขียนโปรแกรมเก็บค่าสตริง2 ค่าขนาดไม่เกิน 30 ตัวอักษรในอาร์เรย์ A และ B • ทำการสลับค่าข้อมูลในตัวแปรอาร์เรย์ทั้งสอง • แสดงผลในค่าอาร์เรย์ทั้งสอง • ใช้ pointer ในการอ้างถึงค่า • เขียนโปรแกรมเก็บค่าสตริง2 ค่าไม่ระบุขนาดโดยใช้พอยเตอร์ A และ B • ทำการสลับค่าข้อมูลในตัวแปรพอยเตอร์ทั้งสอง • แสดงผลในค่าพอยเตอร์ทั้งสอง

  18. Enter string: The representation of objects of the type is hidden from the program units. 15 words Words Length The 3 representation 14 of 2 objects 7 … ... Homework Lab 10 • 1. กำหนดตัวแปรสตริงเพื่อเก็บข้อความขนาดไม่เกิน 80 คำ • 2. ตรวจสอบสตริงว่ามีกี่คำ • 3. เก็บแต่ละคำในอาร์เรย์ของตัวแปรสตริงแบบพอยเตอร์ • 4. แสดงผลลัพท์โดยพิมพ์ • จำนวนคำทั้งหมด • คำ และ ความยาวของคำ

  19. Value of age pointer age How old are you: 18 You are 18 years old. You are 50 years old. #include<stdio.h> void main() { int age; int *pointer; printf(“How old are you : ”); scanf(“%d”,&age); printf(“You are %d years old.\n”,age); pointer = &age; *pointer =50; printf(“You are %d years old. \n, age); } 72009 50 18 Address of age 72009

  20. Pointer & Function • ใช้ส่งผ่านค่าไปยังฟังก์ชั่นได้ • ใช้ในการคืนค่ากลับจากฟังก์ชั่นได้มากกว่า 1 ค่า • ใช้ในการรับส่งค่าอาร์เรย์ให้กับฟังก์ชั่นได้ • เรียกว่า call-by-reference

  21. tmp = p; p = q; q = tmp; Call-by-value สลับค่า (swap) a = 6, b = 2 a = 6, b = 2 #include "stdio.h“ void swap (int p, int q) { int tmp; tmp = p; p = q; q = tmp; } main() { int a = 6, b = 2; printf("a = %d, b = %d\n", a, b); swap(a, b); printf("a = %d, b = %d\n", a, b); }

  22. Call-by-reference a = 6, b = 2 a = 2, b = 6 #include "stdio.h“ void swap (int *p, int *q) { int tmp; tmp = *p; *p = *q; *q = tmp; } main() { int a = 6, b = 2; printf("a = %d, b = %d\n", a, b); swap(&a, &b); printf("a = %d, b = %d\n", a, b); }

  23. a = 6, b = 2 a = 2, b = 6 #include "stdio.h“ void swap (int *, int *) main() { int a = 6, b = 2; printf("a = %d, b = %d\n", a, b); swap(&a, &b); printf("a = %d, b = %d\n", a, b); } void swap (int *p, int *q) { int tmp; tmp = *p; *p = *q; *q = tmp; }

  24. Before num = 20 Now num = 50 After num = 50 Before num = 20 Now num = 50 After num = 20 #include<stdio.h> void change(int p) { p = 50; printf(“Now num = %d\n”, p); } void main() { int num=20; printf(“Before num = %d\n”,num); change(num); printf(“After num = %d\n”, num); } * * * change(&num);

  25. Input number1 : 15 Input number2 : 30 15 30 #include<stdio.h> int num1, num2; void input() { printf(“\n Input number1 : ”); scanf(“%d”,&num1); printf(“\n Input number2 : ”); scanf(“%d”,&num2); } void main() { input(num1,num2); printf(“%d %d”, num1,num2); }

  26. Input number1 : 15 Input number2 : 30 15 30 #include<stdio.h> void input(int a, int b) { printf(“\n Input number1 : ”); scanf(“%d”, a); printf(“\n Input number2 : ”); scanf(“%d”, b); } void main() { int num1, num2; input( num1, num2); printf(“%d %d”, num1,num2); } * * & & & &

  27. #include<stdio.h> void input(int *, int *); int adder(int a, int b) { return a+b; } void main() { int num1, num2,result; input(&num1,&num2); result = add(num1, num2); printf(“%d”, result); } void adder(int a, int b, int *add) { *add = a+b; } add(num1, num2,result);

  28. …^( ^ ^ )^... May the ForcebewithYOU. Thanks, your watching.

More Related