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

created by Dararat Saelee , Computer Sciences Department, Faculty of Science, PSU-HatYai

ต�วช��หรอพอยน�เตอร� ต�วช��หรอพอยน�เตอร�(Pointer)(Pointer)

ต�วช��หรอพอยน�เตอร� ต�วช��หรอพอยน�เตอร�(Pointer)(Pointer)

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

created by Dararat Saelee , Computer Sciences Department, Faculty of Science, PSU-HatYai

2

ต�วช�� ต�วช�� (Pointer)(Pointer) เป�นข�อม�ลชน�ดหน��งท��สามารถสร�างข��น

ในหน!วยความจำ$าหล�กเม�อม�การเร�ยกใช�เท!าน��น ไม!ม�การจำองเน�อท��ไว�ล!วงหน�าเหมอนก�บการประกาศต�วแปรท��ผ!านมา โดยเม�อม�การเร�ยกใช�ข�อม�ลแบบพอยน�เตอร� ระบบจำะจำองเน�อท��ว!างในหน!วยความจำ$าให� หล�งจำากท$างานเสร.จำแล�วก.ต�องม�การคนเน�อท��หน!วยความจำ$าน��นให�แก!ระบบเพ�อให�โปรแกรมส!วน

อ�นสามารถใช�งานเน�อท��น��นได�ต!อไป

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

created by Dararat Saelee , Computer Sciences Department, Faculty of Science, PSU-HatYai

3

Pointer ( Pointer (ต!อต!อ))

ต�วแปรแบบพอยน เตอร ท$าหน�าท��เก.บต$าแหน!งท��อย�! (address) ของต�วแปรหร�อข�อม�ลท��ม�ในหน�วยความจำ�า น��นค�อต�วม�นเองไม�ได�เก#บข�อม�ล เพ�ยงแต�ช�%ไปย�งท��เก#บข�อม�ลอ�กท�หน&�ง เพ��อท�าให�การท�างาน

ของโปรแกรมม�ประส*ทธ*ภาพมากข&%น

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

created by Dararat Saelee , Computer Sciences Department, Faculty of Science, PSU-HatYai

4

Pointer ( Pointer (ต!อต!อ)) ต�วอย!างเช!น score เป�นต�วแปรเก.บค!า

จำ$านวนเต.ม 120 int score; score = 120;

1

2

0

score

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

created by Dararat Saelee , Computer Sciences Department, Faculty of Science, PSU-HatYai

5

Pointer ((ต!อต!อ))

ในขณะท��ถ�าก$าหนด ptr เป�นต�วแปรพอยน�เตอร�ท��ช��ไปย�งข�อม�ล

เลขจำ$านวนเต.ม 120 int *ptr; *ptr = 120;

120ptr784 784

address

เป�นการก$าหนดให�เน�อท��ท��ต�วแปร ptr ช��ไปม�ค!าเท!าก�บ 120

ptr 120หรอ

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

created by Dararat Saelee , Computer Sciences Department, Faculty of Science, PSU-HatYai

6

การประกาศต�วแปรพอยน�เตอร�

ร�ปแบบ type *variable_name เม��อ type = ชน*ดของต�วแปร เช�น int,

float * = เป-นเคร��องหมายท��ก�าหนด

ว�าเป-นต�วแปรแบบ พอยน เตอร

variable_name = ช��อต�วแปรExample int *ptr;

char *pt; float *ft;

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

created by Dararat Saelee , Computer Sciences Department, Faculty of Science, PSU-HatYai

7

การประกาศต�วแปรพอยน�เตอร� ((ต!อต!อ))

Example int *ptr;

char *pt;

float *ft;

intptr111

111

charpt200

200

floatft484

484

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

created by Dararat Saelee , Computer Sciences Department, Faculty of Science, PSU-HatYai

8

เคร�องหมายท��ใช�ก�บพอยน�เตอร�

- เคร��องหมาย & (ampersand)

ใช�ส�าหร�บก�าหนดค�าเร*�มต�นให�ก�บพอยน เตอร โดยการน�าเอาต�าแหน�งท��อย�� (Address) ของต�วแปรในหน�วยความจำ�ามาใช�งาน- เคร��องหมาย *

ใช�ส�าหร�บน�าเอาข�อม�ลในต�าแหน�งท��ต�วแปรพอยน เตอร น�%นช�%อย�� ออกมาใช�งาน

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

created by Dararat Saelee , Computer Sciences Department, Faculty of Science, PSU-HatYai

9

เคร�องหมายท��ใช�ก�บพอยน�เตอร� ((ต!อต!อ))

ต�วอย!าง score เป�นต�วแปรเก.บค!าจำ$านวนเต.ม 120

int score; score = 120; printf(”&score =

%d”,&score);

score

888

120

&score = 888

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

created by Dararat Saelee , Computer Sciences Department, Faculty of Science, PSU-HatYai

10

เคร�องหมายท��ใช�ก�บพอยน�เตอร� ((ต!อต!อ))

Example int *ptr;

จำากร�ป ptr ม�ค!าเท!าก�บ 111 *ptr ม�ค!าเท!าก�บ 52

Example char *pt;

จำากร�ป pt ม�ค!าเท!าก�บ 200 *pt ม�ค!าเท!าก�บ ‘G’

52

ptr

111

111

‘G’pt200

200

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

created by Dararat Saelee , Computer Sciences Department, Faculty of Science, PSU-HatYai

11

ส�ญล�กษณ�และความหมายint *pt ประกาศต�วแปร pt เป�นพอยน�เตอร�ชน�ด integer&a address operator ของ apt = &a; ต�วแปร pt เก.บค!า address operator ของ a*pt ค!าตรงต$าแหน!งท�� pt ช��อย�!b = *ptก$าหนดค!าต�วแปร b เท!าก�บค!าท�� pt ช��อย�!

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

created by Dararat Saelee , Computer Sciences Department, Faculty of Science, PSU-HatYai

12

เคร�องหมายท��ใช�ก�บพอยน�เตอร� ((ต!อต!อ))

Example int a, *b;a = 10;b = &a;

printf (“a=%d”, *b);

a b

10

0100 0370

10 0100

ผลการท�างานa = 10

b = 0100&b = 0370

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

created by Dararat Saelee , Computer Sciences Department, Faculty of Science, PSU-HatYai

13

เคร�องหมายท��ใช�ก�บพอยน�เตอร� (ต!อ)

Example int a, *b;*b = 10;a = *b;

printf (“a=%d”, a);

a b

0100 0370

10 10

ผลการท�างานa = 10

b = 0370&b = ???

*b

Demo Program

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

created by Dararat Saelee , Computer Sciences Department, Faculty of Science, PSU-HatYai

14

คณ�ตศาสตร�ของพอยน�เตอร�

ต�วแปรพอยน�เตอร�สามารถใช�เคร�องหมายทาง คณ�ตศาสตร�มากระท$าได�เช!นเด�ยวก�บต�วแปรท��วๆไป

แต!ไม!สามารถใช�ได�ท��งหมด เคร�องหมายทางคณ�ตศาสตร�ท��ใช�ได� คอ

1. เคร��องหมาย + ค�อ การบวก2. เคร��องหมาย - ค�อ การลบ3. เคร��องหมาย + + ค�อ การเพ*�มค�าคร�%งละ 14. เคร��องหมาย - - ค�อ การลดค�าคร�%งละ 1

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

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

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

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

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

created by Dararat Saelee , Computer Sciences Department, Faculty of Science, PSU-HatYai

17

การส!งพาราม�เตอร�ด�วยต$าแหน!ง

(passing by address)การก$าหนดฟ4งก�ช�น• type function-name (formal parameters)• formal parameters : pointer variables• Ex. int func1 (int *a, int *b) { return *a + *b; }

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

created by Dararat Saelee , Computer Sciences Department, Faculty of Science, PSU-HatYai

18

การส!งพาราม�เตอร�ด�วยต$าแหน!ง

(passing by address)การเร�ยกใช�ฟ4งก�ช�น• var = function-name (arguments)• arguments : address of variables• Ex. z = func1 (&x, &y);

ส!งค!า address ของ x และ y

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

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); }

x

y

5

7

name value address

00140

00142

00140 , 00142

*(00140)+*(00142)

5+7 = 12

12

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

created by Dararat Saelee , Computer Sciences Department, Faculty of Science, PSU-HatYai

20

ª°¥nµ� � 1 µ¦ ¨ nµo°¤¼� � � � #include <stdio.h> void swap (int * , int *); void main() { int a , b ; printf (“enter number1 and number2 : ”); scanf (“%d%d”, &a , &b); swap (&a , &b); printf (“number1 : %d and number2 : %d\n”, a, b); } void swap (int *x , int *y) { int temp; temp = *x; *x = *y; *y = temp; }

Demo program

การเร�ยกใช�ฟ1งช�น

การก�าหนดฟ1งช�น

การส!งพาราม�เตอร�ด�วยต$าแหน!ง

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

created by Dararat Saelee , Computer Sciences Department, Faculty of Science, PSU-HatYai

21

ประโยชน�ของการส!งพาราม�เตอร�ด�วยต$าแหน!ง

• เม�อต�องม�การเปล��ยนแปลงค!าพาราม�เตอร�หล�งจำากการท$างานของฟ4งก�ช�น

• เม�อต�องการส!งค!ากล�บจำากฟ4งก�ช�นมากกว!า1 ค!า

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

created by Dararat Saelee , Computer Sciences Department, Faculty of Science, PSU-HatYai

22

Class Exercise• เข�ยนโปรแกรมเพ��ออ�านค�าส�วนส�งของนศ.จำาก

แฟ3มข�อม�ล แล�วเร�ยกใช�ฟ1งก ช�น 1 ฟ1งก ช�นท��ค�านวณหาค�าส�วนส�งเฉล��ยและค�าส�วนส�งของคนท��ส�งท��ส6ดและแสดงผลท�%ง 2 ค�าท��โปรแกรมหล�ก

• ปร�บโปรแกรมข�างต�นให�ร�บค�าน�%าหน�กและใช�ฟ1งก ช�นเด�ยวก�นท�างานให�เช�นก�น

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

created by Dararat Saelee , Computer Sciences Department, Faculty of Science, PSU-HatYai

23

พอยน�เตอร�และอาร�เรย�พอยน เตอร และอาร เรย ม�ความส�มพ�นธ ก�นอย�างมาก สามารถใช�งานแทนก�นได� ซ&�งการอ�างข�อม�ล

ในอาร เรย สามารถอ�างโดยช��อต�วแปรอาร เรย หร�อ ใช�ต�วแปรพอยน เตอร เพ��อแสดงต�าแหน�ง

ของต�วแปรอาร เรย ก#ได�

ข�อด�ของพอยน เตอร ค�อ การใช�งานเร#วกว�า แลไม�เปล�องเน�%อท�� แต�พอยน เตอร จำะเข�าใจำยากกว�า

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

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];

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

created by Dararat Saelee , Computer Sciences Department, Faculty of Science, PSU-HatYai

25

พอยน�เตอร�และอาร�เรย� (ต!อ) int x[4] = {1, 2, 3, 4}; int

*px[4];

x[0] x[1] x[2] x[3]

px[0] px[1] px[2] px[3]

0210 0212 0214 0216

03EE 03F0 03F2 03F4

1 2 3 4

px[0] = &x[0];px[1] = &x[1];px[2] = &x[2];px[3] = &x[3];

0210 0212 0214 0216

px[0] px[1] px[2] px[3]

03EE 03F0 03F2 03F4

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

created by Dararat Saelee , Computer Sciences Department, Faculty of Science, PSU-HatYai

26

พอยน�เตอร�และอาร�เรย� (ต!อ) int x[4] = {1, 2, 3, 4}; int

*px[4];

x[0] x[1] x[2] x[3]

px[0] px[1] px[2] px[3]

0210 0212 0214 0216

03EE 03F0 03F2 03F4

1 2 3 4

*px[0] = x[0];*px[1] = x[1];*px[2] = x[2];*px[3] = x[3];

0210 0212 0214 0216

px[0] px[1] px[2] px[3]

03EE 03F0 03F2 03F4

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

created by Dararat Saelee , Computer Sciences Department, Faculty of Science, PSU-HatYai

27

ª°¥nµ� � 2 µ¦ nnµÂª ε´®¦º°°µ¦rÁ¦¥rÅ¥¢{r´Ã¥Äoª Ê� � � � � � � � � � � � � � � � � #include <stdio.h> int array[10] = {1, 3, 4, 5, 7, 9, 0, 8, 1, 6}; int sum (int *data , int size); void main() { printf (“sum of data in array = %d\n”, sum (array , 10)); } int sum (int *data , int size) { int sumall = 0; int j; for (j = 0; j < size; j++) { sumall += *data; ++data; } return (sumall); }

Demo program

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

created by Dararat Saelee , Computer Sciences Department, Faculty of Science, PSU-HatYai

28

พอยน�เตอร�และสตร�งสตร*งเป-น Array ท��เก#บต�วอ�กษรเร�ยง

ต�อๆก�นไป และป:ดท�ายด�วย “\0” เพ��อแสดงการส*%นส6ดของข�อความ ด�งน�%นจำ&งสามารถ

ประกาศต�วแปรเป-น Pointer เพ��อเก#บข�อความในล�กษณะเด�ยวก�บ Array ได�เช�นก�นร�ปแบบ char *ch; โดยท��สามารถใช�งานได�เหม�อนก�บ Array

ท��วไป

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

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);

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

created by Dararat Saelee , Computer Sciences Department, Faculty of Science, PSU-HatYai

30

พอยน�เตอร�และต�วแปรชน�ดโครงสร�าง

เราสามารถใช�ต�วแปรพอยน เตอร มาช�%ต�าแหน�งของข�อม�ลหร�อต�วแปรชน*ดโครงสร�าง

ได�เช�นก�นExample struct infor {

char name[15];

float balance;

}; struct infor *p;

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

created by Dararat Saelee , Computer Sciences Department, Faculty of Science, PSU-HatYai

31

พอยน�เตอร�และต�วแปรชน�ดโครงสร�าง (ต!อ)

ในการอ�างถ&งต�วแปร balance ภายในโครงสร�างช��อ infor ได�โดยผ�านต�วแปรพอยน

เตอร ด�งน�%

(*p).balance

หรอ pbalance

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

created by Dararat Saelee , Computer Sciences Department, Faculty of Science, PSU-HatYai

32

การจำ�ดสรรหน!วยความจำ$าแบบไดนาม�ก

ความหมายDynamic Allocation เป-นการจำองพ�%นท��แบบช��วคราวส�าหร�บเก#บค�าของต�วแปร โดยเม��อร�นโปรแกรมต�วแปรจำะย�งไม�ถ�กสร�างข&%น ด�งน�%นพ�%นท��ในหน�วยความจำ�าจำะไม�ถ�กจำองไว�ก�อน เม��อต�องการใช�งานงานแปร จำ&งท�าการ

จำองพ�%นท�� ส�าหร�บเก#บค�าของต�วแปรน�%นข&%นมา และเม��อใช�งานเสร#จำก#สามารถค�นหน�วยความจำ�า

เพ��อใช�งานอย�างอ��น

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

created by Dararat Saelee , Computer Sciences Department, Faculty of Science, PSU-HatYai

33

การจำ�ดสรรพ�นท��ด�วยฟ4งก�ช�น malloc()

ร�ปแบบ*pt = malloc (size)

โดยท�� *pt ต�วแปรพอยน เตอร เพ��อใช�ร�บค�าท��ได�กล�บฟ1งก ช�น malloc() ซ&�งเป-นค�าต�าแหน�งแรกของพ�%นท��ท� %งหมดท��จำองไว� ในกรณ�ท��ไม�สามารถจำองพ�%นท��ได� ฟ1งก ช�นจำะส�งค�า NULL มาให�แทน size ขนาดของพ�%นท��ในหน�วยความจำ�า ท��

ต�องการจำอง ม�หน�วยเป-นไบต

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

created by Dararat Saelee , Computer Sciences Department, Faculty of Science, PSU-HatYai

34

การจำ�ดสรรพ�นท��ด�วยฟ4งก�ช�น malloc()

Exampleint *pt;pt = malloc(8);

Examplefloat *pt;pt = malloc(2*sizeof (float));

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

created by Dararat Saelee , Computer Sciences Department, Faculty of Science, PSU-HatYai

35

การคนพ�นท��ด�วยฟ4งก�ช�น free()

ร�ปแบบfree(pt);

โดยท�� pt ต�วแปรพอยน เตอร ซ&�งเป-นต�วช�%ต�าแหน�งในหน�วยความจำ�าท��เราต�องการค�น

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

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 Infopress DeveloperDeveloper BookBook

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

created by Dararat Saelee , Computer Sciences Department, Faculty of Science, PSU-HatYai

37

Motto Todayท$าด�ต!อคนท��งโลก แต!ไม!เคยท$าด�ก�บ

พ!อแม!ความด�ท��งหลายน��นก.ส�ญเปล!า


Top Related