32
Pointer บทที9 Pointer ใน C++ มีตัวแปรอีกชนิดหนึ่งเรียกวา Pointer ตัวแปร Pointer จะเก็บ Address ของตัวแปร แทนที่จะเก็บขอมูลตางๆ เหมือนตัวแปรชนิดอื่นๆ จากคุณสมบัติของ ตัวแปรชนิด Pointer จึงมองดู เหมือนกับ ตัวชีหรือพอยนเตอร ซึ่งชี้ไปทีAddress ของตัวแปร Address คืออะไร ในการทํจะเก็บขอมูล(data)ของคอมพิวเตอร และโปรแกรมหรือชุดของ คําสั่ง (instruction set)ไวในหนวยความจําหรือที่เรียกวา เมโมรี (memory) เพื่อนําไปประมวลผลใน CPU หรือเก็บผลการทําจาก CPU หนวยความจํานี้มีหนวยนับเปน ไบต (byte) หนวยความจําทุก หนวยหรือไบตจะมีเลขประจําตําแหนงเพื่อใชสําหรับอางถึงหมายเลข ตําแหนงของหนวยความจํา เปรียบเสมือนบานเลขที่ที่เราใชอางถึงในการสงจดหมาย หมายเลขตําแหนง หนวยความจํา หนวยหนึ่งเราเรียกวา แอดเดรส (address) ของหนวยความจํา โดยหมายเลขของแอดเดรสจะเริ่ม ตําแหนงที0, 1, 2, 3,…จนถึงคาสูงสุดของหนวยความจําที่ติดตั้งอยูในคอมพิวเตอรในเครื่อง นั้น การกําหนดตําแหนงหนวยความจําเมื่อ C++ มีการประกาศใชตัวแปรซึ่งจะตองจองพื้นที่ใน หนวยความจํา C++ Compiler จะดําเนินการจองหรือกําหนดตําแหนงแอดเดรสและจัดสรร หนวยความจําผานซอฟตแวรระบบปฏิบัติการ (Operating System) และเปนทําหนาที่ของ ระบบปฏิบัติการโดยตรงและเราก็สามารถเขียนโปรแกรมเกี่ยวกับแอดเดรส (address) ไดเชนกัน 9.1 การกําหนดตัวแปร Pointer การกําหนดตัวแปร Pointer จะคลายกับการกําหนดตัวแปรชนิดตางๆ เพียงแตตองมี เครื่องหมาย * หนาชื่อตัวแปร ดังนีint *pt; ในที่นี้กําหนดให pt เปนตัวแปร Pointer ซึ่งเก็บ Address ของตัวแปรชนิดตัวเลขจํานวนเต็ม ในเรื่อง Pointer มีเครื่องหมาย 2 ชนิด คือ * และ & เครื่องหมาย * จะใหคา ของขอมูล ซึ่งเก็บ อยูใน Address โดย Address นี้เก็บ อยูในตัวแปร Pointer ซึ่งอยูหลังเครื่องหมาย * สําหรับ เครื่องหมาย & จะใหคา Address ของตัวแปรซึ่งอยูหลังเครื่องหมาย & ดังตัวอยาง 322 112 Introduction to Programming ผศ.อุรฉัตร โคแกว 1

บทที่ 9 Pointer · Pointer บทที่ 9 Pointer ใน C++ มีตัีวแปรอกชนิดหนึ่ างเร Pointer ียกวตัวแปร

  • Upload
    others

  • View
    12

  • Download
    0

Embed Size (px)

Citation preview

Page 1: บทที่ 9 Pointer · Pointer บทที่ 9 Pointer ใน C++ มีตัีวแปรอกชนิดหนึ่ างเร Pointer ียกวตัวแปร

Pointer

บทที่ 9 Pointer

ใน C++ มีตัวแปรอีกชนิดหนึ่งเรียกวา Pointer ตัวแปร Pointer จะเก็บ Address ของตัวแปร แทนที่จะเก็บขอมูลตางๆ เหมือนตัวแปรชนิดอื่นๆ จากคุณสมบัติของ ตัวแปรชนิด Pointer จึงมองดูเหมือนกับ ตัวช้ี หรือพอยนเตอร ซ่ึงชี้ไปที่ Address ของตัวแปร

Address คืออะไร ในการทํจะเก็บขอมูล(data)ของคอมพิวเตอร และโปรแกรมหรือชุดของคําสั่ง (instruction set)ไวในหนวยความจําหรือท่ีเรียกวา เมโมรี (memory) เพื่อนําไปประมวลผลใน CPU หรือเก็บผลการทําจาก CPU หนวยความจํานี้มีหนวยนับเปน ไบต (byte) หนวยความจําทุกหนวยหรือไบตจะมีเลขประจําตําแหนงเพื่อใชสําหรับอางถึงหมายเลข ตําแหนงของหนวยความจําเปรียบเสมือนบานเลขที่ที่เราใชอางถึงในการสงจดหมาย หมายเลขตําแหนง หนวยความจําหนวยหนึ่งเราเรียกวา แอดเดรส (address) ของหนวยความจํา โดยหมายเลขของแอดเดรสจะเริ่มตําแหนงที่ 0, 1, 2, 3,…จนถึงคาสูงสุดของหนวยความจําที่ติดตั้งอยูในคอมพิวเตอรในเครื่อง นั้น ๆ

การกําหนดตําแหนงหนวยความจําเมื่อ C++ มีการประกาศใชตัวแปรซึ่งจะตองจองพืน้ทีใ่น หนวยความจํา C++ Compiler จะดําเนินการจองหรือกําหนดตําแหนงแอดเดรสและจัดสรรหนวยความจําผานซอฟตแวรระบบปฏิบัติการ (Operating System) และเปนทําหนาที่ของระบบปฏิบัติการโดยตรงและเราก็สามารถเขียนโปรแกรมเกี่ยวกับแอดเดรส (address) ไดเชนกัน

9.1 การกําหนดตัวแปร Pointer

การกําหนดตัวแปร Pointer จะคลายกับการกําหนดตัวแปรชนิดตางๆ เพียงแตตองมีเครื่องหมาย * หนาชื่อตัวแปร ดังนี้

int *pt;

ในที่นี้กําหนดให pt เปนตัวแปร Pointer ซ่ึงเก็บ Address ของตัวแปรชนิดตัวเลขจํานวนเต็ม

ในเรื่อง Pointer มีเครื่องหมาย 2 ชนิด คือ * และ & เครื่องหมาย * จะใหคา ของขอมูล ซ่ึงเก็บอยูใน Address โดย Address นี้เก็บ อยูในตัวแปร Pointer ซ่ึงอยูหลังเครื่องหมาย * สําหรับเครื่องหมาย & จะใหคา Address ของตัวแปรซึ่งอยูหลังเครื่องหมาย & ดังตัวอยาง

322 112 Introduction to Programming ผศ.อุรฉัตร โคแกว 1

Page 2: บทที่ 9 Pointer · Pointer บทที่ 9 Pointer ใน C++ มีตัีวแปรอกชนิดหนึ่ างเร Pointer ียกวตัวแปร

Pointer

ตัวอยาง โปรแกรมที่ 1 #include "stdio.h"

main() {

int *pt, a, b;

a = 20;

pt = &a;

b = *pt;

printf("%d\n",b);

}

ผลลัพธ 20

โปรแกรมที่ 1 กําหนดใหตัวแปร pt เปนชนิด Pointer เปนการกําหนด คา Address ของตัวแปร a ใหกับตัวแปรชนิด Pointer pt หรือเปนการกําหนดให Pointer pt ช้ีไปที่ Address ของตัวแปร a และมีสวนของการกําหนด คาที่เก็บใน Address ซ่ึงถูกชี้ โดย Pointer pt ไปเก็บในตัวแปร b นั่นคือตัวแปร b จะมีคาเปน 20

เราอาจจะแสดงคาที่เก็บอยูใน Address ซ่ึงถูกชี้โดย Pointer pt ไดโดยตรง ใน Function printf() ดังนี้ printf("%d\n".*pt);

การกําหนดคาโดยผานตัวแปร Pointer ดังในโปรแกรมที่ 1 เปนการกําหนดคาโดยวิธีออม(Indirection)

322 112 Introduction to Programming ผศ.อุรฉัตร โคแกว 2

Page 3: บทที่ 9 Pointer · Pointer บทที่ 9 Pointer ใน C++ มีตัีวแปรอกชนิดหนึ่ างเร Pointer ียกวตัวแปร

Pointer

ตัวอยาง โปรแกรมที่ 2 #include "stdio.h"

main()

{ int *pt,a;

a = 20;

pt = &a;

printf("Address pointed by pointer pt stored value of %d\n",*pt);

*pt = 10;

printf("After indirection assignment\n");

printf("a = %d\n",a);

} ผลลัพธ Address pointed by pointer pt stored value of 20

After indirection assignment

a = 10

เครื่องหมาย * ซ่ึงอยูหนาตัวแปร Pointer ซ่ึงอยูทางดานซาย ของเครื่องหมาย = จะหมายถึง การกําหนดคาใหตัวแปร ซ่ึง Address ของมันเก็บ อบูในตัวแปร Pointer (หรือ Address ถูกชี้โดย Pointer) ในโปแกรมที่ 2 Address ของตัวแปร a ถูกชี้โดย Pointer pt ในตอนเริ่มแรก ตัวแปร a มีคาเปน 20 ในบรรทัดที่ 8 เปนการกําหนดคา 10 ใหกับตัวแปร a โดยวิธีออม นั่นคือเมื่อส้ินสุดโปรแกรม ตัวแปร a จะมีคาเปลี่ยนเปน 10

322 112 Introduction to Programming ผศ.อุรฉัตร โคแกว 3

Page 4: บทที่ 9 Pointer · Pointer บทที่ 9 Pointer ใน C++ มีตัีวแปรอกชนิดหนึ่ างเร Pointer ียกวตัวแปร

Pointer

โปรแกรมที่ 3 กําหนดใหตัวแปร Pointer pt เก็บ Address ของชนิดขอมูล ตัวเลขจํานวนจรงิ ในโปแกรมไดกําหนด คา Address ของตัวแปร a ซ่ึงเปนชนิดเลขจํานวนเต็ม ใหกับตัวแปร Pointer fpt นั่นคือตัวแปร Pointer fpt จะเก็บ Address ซ่ึงสามารถ รับขอมูลไดเพียง 2 Byte เทานั้น เมื่อกําหนดเลขจํานวนจริง ซ่ึงมีขนาด 4 Byte ใหกับตัวแปร a โดยวิธีออม จะเกิดการเขียนทับขอมูลใน 2 Byte ที่อยูติดกับ Address ของตัวแปร a ทําใหตัวแปร a มีคาไมถูกตอง

ตัวอยาง โปรแกรมที่ 3 #include "stdio.h"

main()

{ float *fpt;

int a;

fpt = &a;

*fpt = 10.89

printf("%d\n",a);

}

ผลลัพธ 15729

โปรแกรมที่ 3 เมื่อ Compile จะไมมีขอผิดพลาด แตจะมีขอความเตือน ในหนาตาง Warning วา "Warning c:\prg\Prg08-03.cpp:Suspicious pointer Conversion in function main"

322 112 Introduction to Programming ผศ.อุรฉัตร โคแกว 4

Page 5: บทที่ 9 Pointer · Pointer บทที่ 9 Pointer ใน C++ มีตัีวแปรอกชนิดหนึ่ างเร Pointer ียกวตัวแปร

Pointer

ใน C++ ตัวแปร Pointer ซ่ึงไมไดเก็บคา Address ของตัวแปรใดๆ จะมีคาเปน Null หรือหมายความวา Pointer ไมไดชี้ไปท่ี Address ของตัวแปรใดๆ ในโปแกรมที่ 4 จะกําหนดคาโดยวิธีออม ที่ไมไดกําหนด Address ใหกับตัวแปร Pointer กอน เมื่อ Compile จะมีขอความเตือน "Warning c:\prg\Prg08-04.cpp:Possible use of ipt before definition in function main"

ตัวอยาง โปรแกรมที่ 4 #include "stdio.h"

main()

{

int *ipt;

*ipt = 40;

printf("%d\n",*pt);

}

ผลลัพธ 40

Null pointer assignment 9.2 คณิตศาสตรของ Pointer

เครื่องหมายคณิตศาสตร ที่ใชกับตัวแปร Pointer ไดแก +,++,- และ -- ตัวแปรหรือคาคงที่ ที่จะนํามาบวก หรือ ลบ กับตัวแปร Pointer จะเปนชนิด เลขจํานวนเต็มเทานั้น

การเพิ่มคาของตัวแปร Pointer โดยการบวก จะหมายถึงการเพิ่มคา Address ที่เก็บอยูในตัวแปร Pointer หรือเปนการเลื่อน Pointer ช้ีไปที่ Address ที่สูงขึ้น ทั้งนี้ขึ้นกับชนิด ของตัวแปรซึ่งถูกชี้โดย Pointer ตัวอยางเชน ตัวแปร Pointer pt เก็บ Address ของตัวแปรชนิด เลขจํานวนเต็ม a สมมติใหตัวแปร a อยูที่ Address 40 เมื่อคําสั่ง ptt++; ถูกประมวล Pointer pt จะชี้ไปที่ Address 42 (เพิ่ม 2 Byte) ถาตัวแปร a เปนชนิดเลขจํานวนจริง Pointer pt จะชี้ไปที่ Address 44 (เพิ่มขึ้น 4 Byte)

322 112 Introduction to Programming ผศ.อุรฉัตร โคแกว 5

Page 6: บทที่ 9 Pointer · Pointer บทที่ 9 Pointer ใน C++ มีตัีวแปรอกชนิดหนึ่ างเร Pointer ียกวตัวแปร

Pointer

สมมติ วาตองการให Pointer pt ช้ีไปที่ Address 100 จะตองเพิ่ม คาของ Pointer pt อีก 60 ดังนี้

pt = pt+60;

สําหรับเครื่องหมาย - และ -- จะลดคา Address ที่เก็บอยูในตัวแปร Pointer หรือกําหนดให Pointer ช้ีไปที่ Address ที่มีคาต่ําลง ดังนี้ pt --;

คําสั่งนี้จะทําให Pointer pt ช้ีไปที่ Address 38 (ลดลง 2 Byte) Pointer ของตัวแปรชนิดอักษร ซ่ึงใช 1 Byte ในการเก็บตัวอักษร 1 ตัว การเพิ่ม และลด คาของ Address ในตัวแปร Pointer โดยเครื่องหมาย ++ และ -- จะทําใหคาของ Address ของตัวแปร Pointer เพิ่มขึ้น และลดลง 1 ตามลําดับ

โปรแกรมที่ 5 จะแสดงคาของตัวแปร Pointer ipt, fpt,cpt และ dpt กอน และหลัง เพิ่มคา Address

ตัวอยาง โปรแกรมที่ 5 #include "stdio.h"

main()

{

int *ipt,i;

float *fpt,f;

char *cpt, c;

double *dpt,d;

ipt = &i;

fpt = &f;

cpt = &c;

dpt = &d;

322 112 Introduction to Programming ผศ.อุรฉัตร โคแกว 6

Page 7: บทที่ 9 Pointer · Pointer บทที่ 9 Pointer ใน C++ มีตัีวแปรอกชนิดหนึ่ างเร Pointer ียกวตัวแปร

Pointer

printf("Address 's value before increasing\n");

printf("ipt = %p\n",ipt);

printf("fpt = %p\n",fpt);

printf("cpt = %p\n",cpt);

printf("dpt = %p\n",dpt);

ipt++;

fpt++;

cpt++;

dpt++;

printf("\n");

printf("Address 's value after increasing\n");

printf("ipt = %p\n",ipt);

printf("fpt = %p\n",fpt);

printf("cpt = %p\n",cpt);

printf("dpt = %p\n",dpt);

}

ผลลัพธ Address 's value before increasing

322 112 Introduction to Programming ผศ.อุรฉัตร โคแกว 7

Page 8: บทที่ 9 Pointer · Pointer บทที่ 9 Pointer ใน C++ มีตัีวแปรอกชนิดหนึ่ างเร Pointer ียกวตัวแปร

Pointer

ipt = FFCC

fpt = FFCE

cpt = FFD5

dpt = FFD8

Address 's value after increasing

ipt = FFCE

fpt = FFD2

cpt = FFD6

dpt = FFE0

โปแกรมที่ 6 จะแสดงคาของ ตัวแปร Pointer ipt, fpt, cpt และ dpt กอนและหลัง ลดคา Address

ตัวอยาง โปรแกรมที่ 6 #include "stdio.h"

main() {

int *ipt,i;

float *fpt,f;

char *cpt, c;

double *dpt,d;

322 112 Introduction to Programming ผศ.อุรฉัตร โคแกว 8

Page 9: บทที่ 9 Pointer · Pointer บทที่ 9 Pointer ใน C++ มีตัีวแปรอกชนิดหนึ่ างเร Pointer ียกวตัวแปร

Pointer

ipt = &i;

fpt = &f;

cpt = &c;

dpt = &d;

printf("Address 's value before decreasing\n");

printf("ipt = %p\n",ipt);

printf("fpt = %p\n",fpt);

printf("cpt = %p\n",cpt);

printf("dpt = %p\n",dpt);

ipt--;

fpt--;

cpt--;

dpt--;

printf("\n");

printf("Address 's value after decreasing\n");

printf("ipt = %p\n",ipt);

printf("fpt = %p\n",fpt);

printf("cpt = %p\n",cpt);

printf("dpt = %p\n",dpt);

322 112 Introduction to Programming ผศ.อุรฉัตร โคแกว 9

Page 10: บทที่ 9 Pointer · Pointer บทที่ 9 Pointer ใน C++ มีตัีวแปรอกชนิดหนึ่ างเร Pointer ียกวตัวแปร

Pointer

} ผลลัพธ Address 's value before decreasing

ipt = FFCC

fpt = FFCE

cpt = FFD5

dpt = FFD8

Address 's value after decreasing

ipt = FFCA

fpt = FFDA

cpt = FFD4

dpt = FFD0 ตัวอยาง โปรแกรมที่ 7 #include "stdio.h"

main()

{

int *pt,j;

pt = &j;

*pt = 100;

322 112 Introduction to Programming ผศ.อุรฉัตร โคแกว 10

Page 11: บทที่ 9 Pointer · Pointer บทที่ 9 Pointer ใน C++ มีตัีวแปรอกชนิดหนึ่ างเร Pointer ียกวตัวแปร

Pointer

printf("Before increasing\n");

printf("pt = %p\n",pt);

printf("j = %d\n",j);

*pt++;

printf("After increasing\n");

printf("pt = %p\n",pt);

printf("j = %d\n",j);

}

ผลลัพธ Before increasing

pt = FFDE

j = 100

After increasing

pt = FFE0

j = 100

คําสั่ง *pt++ ในโปรแกรมที่ 7 จะเพิ่มคา Address ในตัวแปร Pointer pt กอน แลวจึงใหคาของขอมูลที่เก็บอยูใน Address ใหม ขอใหสังเกตวา ตัวแปร Pointer pt จะมีคาเพิ่มชึ้น แตตัวแปร j จะมีคาเทาเดิม

322 112 Introduction to Programming ผศ.อุรฉัตร โคแกว 11

Page 12: บทที่ 9 Pointer · Pointer บทที่ 9 Pointer ใน C++ มีตัีวแปรอกชนิดหนึ่ างเร Pointer ียกวตัวแปร

Pointer

ตัวอยาง โปรแกรมที่ 8 #include "stdio.h"

main()

{

int *pt, j ;

pt = &j;

*pt = 100;

printf("Before increasing\n");

printf("pt = %p\n",pt);

printf("j = %d\n",j);

(*pt)++;

printf("After increasing\n");

printf("pt = %p\n",pt);

printf("j = %d\n",j);

}

ผลลัพธ Before increasing

pt = FFDE

j = 100

322 112 Introduction to Programming ผศ.อุรฉัตร โคแกว 12

Page 13: บทที่ 9 Pointer · Pointer บทที่ 9 Pointer ใน C++ มีตัีวแปรอกชนิดหนึ่ างเร Pointer ียกวตัวแปร

Pointer

After increasing

pt = FFDE

j = 101

คําสั่ง (*pt)++ ในโปรแกรมที่ 8 จะเพิ่มคาของตัวแปร ซ่ึง Address ของมันถูกเก็บในตัวแปร Pointer pt ในที่นี้ เพิ่มคาของตัวแปร j อีก 1 เปน 101 ขอใหสังเกตวา เราใชเครื่องหมาย ++ ในโปรแกรมที่ 8 คาของตัวแปร j จะเพิ่มขึ้น แตคาของตัวแปร Pointer pt จะมีคาคงเดิม

9.3 ความสัมพันธของ Pointer และ Array

ใน C++ Pointer และ Array จะมีความสัมพันธกันโดย เมื่อใช Array โดยไมระบุ Index เลย Array จะทําหนาที่ เสมือนเปน Pointer ซ่ึงชี้ไปที่ สวนตนของ Array (ตัวแปร Array ตัวแรก) ดังเชนใน Function gets() ซ่ึงเราจะเขียนเฉพาะชื่อ Array เทานั้น Function gets() จะนําตัวอักษรที่ปอนทางแปนพิมพ ไปเก็บใน Array ซ่ึงถูกชี้โดย Pointer การผานคาไป Function ใน C++ จะผานในรูปของ Pointer เทานั้น

ในโปรแกรมที่ 9 คําสั่ง pt = number จะทําให Pointer pt ช้ีไปที่สวนตนของ Array number ในการพิมพคาของตัวแปร Array แตละตัว ทําไดโดย เพิ่มคา Address ในตัวแปร Pointer หรือเล่ือน Pointer ไปยัง Address ที่สูงขึ้น ซ่ึงอยูติดกัน

ตัวอยาง โปรแกรมที่ 9 #include "stdio.h"

main()

{

int number[3];

int *pt;

322 112 Introduction to Programming ผศ.อุรฉัตร โคแกว 13

Page 14: บทที่ 9 Pointer · Pointer บทที่ 9 Pointer ใน C++ มีตัีวแปรอกชนิดหนึ่ างเร Pointer ียกวตัวแปร

Pointer

number[0] = 100;

number[1] = 200;

number[2] = 300;

pt = number;

printf("Number[0] = %d\n",*pt);

printf("Number[1] = %d\n",*(pt+1));

printf("Number[2] = %d\n",*(pt+2));

}

ผลลัพธ number[0] = 100

number[1] = 200

number[2] = 300

322 112 Introduction to Programming ผศ.อุรฉัตร โคแกว 14

Page 15: บทที่ 9 Pointer · Pointer บทที่ 9 Pointer ใน C++ มีตัีวแปรอกชนิดหนึ่ างเร Pointer ียกวตัวแปร

Pointer

ในโปรแกรมที่ 10 จะใหผลลัพธเหมือนโปรแกรมที่ 9 แตใชคําส่ัง for สรางลูปในการพิมพคา

ตัวอยาง โปรแกรมที่ 10 #include "stdio.h"

main() {

int number[3];

int *pt;

number[0] = 100;

number[1] = 200;

number[2] = 300;

pt = number;

for(int i = 0;i<=2;i++)

printf("Number[%d] = %d\n",i,*(pt+i));

}

} ผลลัพธ number[0] = 100

number[1] = 200

number[2] = 300

ในโปรแกรมที่ 9 และ 10 จะตองใชเครื่องหมาย ( ) ครอมนิพจนของ Pointer ทั้งนี้เพราะ เครื่องหมาย * มีลําดับสูงกวาเครื่องหมาย + ดังนั้นจึงตองระวังในการเขียนนิพจนของ Pointer

322 112 Introduction to Programming ผศ.อุรฉัตร โคแกว 15

Page 16: บทที่ 9 Pointer · Pointer บทที่ 9 Pointer ใน C++ มีตัีวแปรอกชนิดหนึ่ างเร Pointer ียกวตัวแปร

Pointer

ในโปรแกรมที่ 11 จะใหผลลัพธเหมือน โปรแกรมที่ 10 ในการอางถึงตัวแปร Array แตละตัว สามารถใช Pointer แทนได เสมือนหนึ่งเปน Array ขอใหสังเกตวา *(pt+i) จะใหผลลัพธเหมือนกับ pt[i]

ในการใช Pointer ในลักษณะของ Array ดังในโปรแกรมที่ 11 จะใชไดเฉพาะ เมื่อ Pointer ช้ีไปที่ Array เทานั้น

ตัวอยาง โปรแกรมที่ 11 // pointer prog.11

#include "stdio.h"

#include "iostream.h"

main()

{ int number[3];

int *pt;

number[0] = 100;

number[1] = 200;

number[2] = 300;

pt = number;

for(int i = 0;i<=2;i++)

printf("Number[%d] = %d\n",i,pt[i]);

cin.get();

}

322 112 Introduction to Programming ผศ.อุรฉัตร โคแกว 16

Page 17: บทที่ 9 Pointer · Pointer บทที่ 9 Pointer ใน C++ มีตัีวแปรอกชนิดหนึ่ างเร Pointer ียกวตัวแปร

Pointer

ผลลัพธ number[0] = 100

number[1] = 200

number[2] = 300 ตัวอยาง โปรแกรมที่ 12 #include "stdio.h"

main() {

int y[3][3] = {

10,20,30,

40,50,60,

70,80,90 } ;

int *pt,i,j;

pt = y;

for(i = 0;i<=2;i++){

for(j = 0;j<=2;j++){

printf("y[%d][%d] = %d\n",i,j,*(pt+(i*3)+j));

}

}

}

322 112 Introduction to Programming ผศ.อุรฉัตร โคแกว 17

Page 18: บทที่ 9 Pointer · Pointer บทที่ 9 Pointer ใน C++ มีตัีวแปรอกชนิดหนึ่ างเร Pointer ียกวตัวแปร

Pointer

ผลลัพธ y[0][0] = 10

y[0][1] = 20

y[0][2] = 30

y[1][0] = 40

y[1][1] = 50

y[1][2] = 60

y[2][0] = 70

y[2][1] = 80

y[2][2] = 90

โปรแกรมที่ 12 ใช Pointer แสดงคาตางๆ ที่เก็บไวใน Array y ซ่ึงมีขนาด 3 x 3 ในการระบุตัวแปร Array สําหรับ Array ที่มีมิติ มากกวา 1 โดยใช Pointer จะตองคูณตําแหนงของแถวนอน ของตัวแปร Array เร่ิมตนดวย จํานวนแถวตั้งของ Array แลวบวกดวยตําแหนงของแถวตั้ง ของตัวแปร Array สมมติตองการระบุตัวแปร Array y[3][2] เมื่อใช Pointer จะไดดังนี้

*(pt+(3*3)+2)

โปรแกรมที่ 12 เมื่อ Compile แลว จะมีขอความเตือนในหนาตาง Warning ดังนี้ "Waring C:\Prg\Prg08-012.cpp: Suspicious pointer conversion in function main" เมื่อทําการ Link จะไมมีขอผิดพลาด และโปรแกรมทํางานไดอยางถูกตอง

322 112 Introduction to Programming ผศ.อุรฉัตร โคแกว 18

Page 19: บทที่ 9 Pointer · Pointer บทที่ 9 Pointer ใน C++ มีตัีวแปรอกชนิดหนึ่ างเร Pointer ียกวตัวแปร

Pointer

9.4 ความสัมพันธของ Pointer และ String

ในหัวขอที่ผานมา เราไดทราบถึงการนํา Pointer ไปใชกับ Array ในหัวขอนี้ จะขอกลาวถึงการนํา Pointer ไปใชกับ String พิจารณาโปแกรมที่ 13 ซ่ึงกําหนดคา เริ่มแรก ใหกับ Array String พิจารณาโปแกรมที่ 13 ซ่ึงกําหนดคา เร่ิมแรกใหกับ String str บรรทัดที่ มีคําสั่ง spt = str; ซ่ึงจะทําให Pointer spt ช้ีไปที่สวนตนของ Array String str คําสั่ง for ซ่ึงใชสราง ลูป ในการพิมพขอมูลใน Array มีส่ิงที่ควรจะทราบ คือ การทดสอบเงื่อนไข โดยจะทดสอบคาที่ถูกชี้ โดย Pointer spt วามี คาเปน String null หรือไม ถาเปน String null เงื่อนไขจะเปนเท็จ (Array ที่เก็บขอมูล String จะปดทายดวย String null เสมอ) ทําใหการวนลูปสิ้นสุด

ตัวอยาง โปรแกรมที่ 13 #include "stdio.h"

main()

{

char str[6] = "Hello";

char *spt;

int i;

spt = str;

for(i = 0;spt[i];++i){

Printf("%c",spt[i]);

}

} ผลลัพธ Hello

322 112 Introduction to Programming ผศ.อุรฉัตร โคแกว 19

Page 20: บทที่ 9 Pointer · Pointer บทที่ 9 Pointer ใน C++ มีตัีวแปรอกชนิดหนึ่ างเร Pointer ียกวตัวแปร

Pointer

โปรแกรมที่ 14 จะใหผลลัพธ เหมือนโปรแกรมที่ 13 แตใชคําสั่ง while สรางลูป ในการพิมพขอมูล while(*spt) จะทีคาเปนเท็จก็ตอเมื่อ Pointer spt เล่ือนไปชี้ที่ Address ซ่ึงเก็บคา String Null

ตัวอยาง โปรแกรมที่ 14 #include "stdio.h"

main()

{

char str[6] = "Hello";

char *spt;

spt = str;

while(*spt){

printf("%c",*spt);

spt++;

}

}

ผลลัพธ Hello

322 112 Introduction to Programming ผศ.อุรฉัตร โคแกว 20

Page 21: บทที่ 9 Pointer · Pointer บทที่ 9 Pointer ใน C++ มีตัีวแปรอกชนิดหนึ่ างเร Pointer ียกวตัวแปร

Pointer

โปรแกรมที่ 15 ใชคุณสมบัติ ของ Pointer ในการหาความยาวของ String

ตัวอยาง โปรแกรมที่ 15 #include "stdio.h"

#include "conio.h"

main()

{

int i; ;

char str[30] = "";

char *spt;

i = 0;

spt = str;

printf("Enter strings not more than 29 characters : ");

gets(str);

while(*spt){ i++;

spt++;

}

printf("%s has %d characters\n",str,i);

getch();

}

322 112 Introduction to Programming ผศ.อุรฉัตร โคแกว 21

Page 22: บทที่ 9 Pointer · Pointer บทที่ 9 Pointer ใน C++ มีตัีวแปรอกชนิดหนึ่ างเร Pointer ียกวตัวแปร

Pointer

ผลลัพธ Enter strings not more than 29 characters : Thailand

Thailand has 8 characters

โปรแกรมที่ 16 ใชคุณสมบัติของ Pointer แสดงขอความในลักษณะ กลับกันกับขอความเดิม คําสั่ง while ตัวแรกจะเลื่อน Pointer spt ไปที่สวนทายของ Array String str (ช้ีไปที่ Address ของ String null) คําสั่ง while ตัวหลังจะสรางลูป สําหรับพิมพอักษรแตละตัวใน Array string str ขอใหสังเกตวา กอนที่จะเขาสูคําสั่ง while ตัวหลัง จะตองเล่ือน Pointer spt กลับมา 1 คา Address กอน บรรทัดที 15-18 ในโปแกรมที่ 16 สามารถเขียนใหมไดดังนี้

while(i){

printf("%c",*spt--);

i--;

}

ตัวอยาง โปรแกรมที่ 16 #include "stdio.h"

main() { char str[30],*spt;

int i;

i = 0;

printf("Enter strings : ");

gets(str);

322 112 Introduction to Programming ผศ.อุรฉัตร โคแกว 22

Page 23: บทที่ 9 Pointer · Pointer บทที่ 9 Pointer ใน C++ มีตัีวแปรอกชนิดหนึ่ างเร Pointer ียกวตัวแปร

Pointer

spt = str;

while(*spt){ i++;

spt++; }

spt--;

while(i){

printf("%c",*spt);

spt--;

i--;

}

}

ผลลัพธ Enter stringd : Thailand

dnaliahT ตัวอยาง โปรแกรมที่ 17 #include "stdio.h"

main()

{

322 112 Introduction to Programming ผศ.อุรฉัตร โคแกว 23

Page 24: บทที่ 9 Pointer · Pointer บทที่ 9 Pointer ใน C++ มีตัีวแปรอกชนิดหนึ่ างเร Pointer ียกวตัวแปร

Pointer

char *str;

str = "Made in Thailand.";

printf(str);

}

ผลลัพธ Made in Thailand.

ในโปรแกรมที่ 17 กําหนดคาคงที่ String หรือขอความซึ่งอยูภายใน เครื่อง " " ใหกับตัวแปร Pointer ในลักษณะนี้ จะทําให C++ เก็บคาคงที่ String ดังกลาวใน ตาราง String และ Pointer จะเล่ือนไปชี้ที่ Address ของคาคงที่ String ซ่ึงสามารถสามารถ พิมพคาคงที่ String โดยใช Function printf()

9.5 Array ของ Pointer

Pointer สามารถกําหนดใหเปน Array ได โดยใน Array จะมีตัวแปร Pointer เหมือนตัวแปร Array ทั่วไป Array ของ Pointer จะเก็บ Address ของตัวแปร ตามชนิดขอมูลตัวแปร ในการกําหนด Array ของ Pointer มีรูปแบบดังนี้

Data type *pointer namr[size];

ตัวอยางเชน int *iap[5];

Array ของ Pointer iap จะมีตัวแปร Pointer 5 ตัวสําหรับเก็บ Address ของตัวแปรชนิดตัวชนิดเลขจํานวนเต็ม ถาตองการใหตัวแปร Pointer ตัวที่ 2 เก็บ Address ของตัวแปร ชนิดเลขจํานวนเต็ม x จะเขียนดังนี้

322 112 Introduction to Programming ผศ.อุรฉัตร โคแกว 24

Page 25: บทที่ 9 Pointer · Pointer บทที่ 9 Pointer ใน C++ มีตัีวแปรอกชนิดหนึ่ างเร Pointer ียกวตัวแปร

Pointer

iap[2] =&x;

ในโปแกรมที่ 18 กําหนดให pt เปน Array ของ Pointer ซ่ึงมีตัวแปร Pointer 3 ตัว สําหรับเก็บ Address ของตัวแปรชนิดเลขจํานวนเต็ม ตัวแปร Pointer ตัวแปร Pointer แตละตัว ถูกกําหนดคาดวย คา Address ของตัวแปรแตละตัวใน Array x จากนั้นจึงเพิ่มคาของตัวแปร Array x ทุกตัวแลวเก็บใน ตัวแปร Array เดิม

ตัวอยาง โปรแกรมที่ 18 #include "stdio.h"

main()

{

int *pt[3],i;

int x[] = {10,20,30};

for(i=0;i<=2;++i){

ot[i] = &x[i];

}

for(i=0;i<=2;++i){

*pt[i] = x[i]+10;

printf("x[%d] = %d\n",i,*pt[i]);

}

}

322 112 Introduction to Programming ผศ.อุรฉัตร โคแกว 25

Page 26: บทที่ 9 Pointer · Pointer บทที่ 9 Pointer ใน C++ มีตัีวแปรอกชนิดหนึ่ างเร Pointer ียกวตัวแปร

Pointer

ผลลัพธ x[0] = 20

x[1] = 30

x[2] = 40 ตัวอยาง โปรแกรมที่ 19 #include "stdio.h"

main()

{

char *country_capital[][2] = {

"Thailand", "Bangkok",

"Japan","Tokyp",

"England","London",

"",""

};

int i,j;

printf("%15s%26s\n\n","Country","Capital");

for(i = 0;*country_capital[i][0];i++){

for(j=0;j<=1;j++){

printf("%-18s",country_capital[i][j]);

}

322 112 Introduction to Programming ผศ.อุรฉัตร โคแกว 26

Page 27: บทที่ 9 Pointer · Pointer บทที่ 9 Pointer ใน C++ มีตัีวแปรอกชนิดหนึ่ างเร Pointer ียกวตัวแปร

Pointer

printf("\n");

}

} ผลลัพธ Country Capital

Thailand Bangkok

Japan Tokyo

England London

ในโปรแกรมที่ 19 กําหนดคาเริ่มแรกใหกับ Array ของ Pointer ซ่ึงเหมือนกับการกําหนดคาเร่ิมแรก ใหกับ Array ทัวไป โปรแกรมนี้จะใหผลลัพธเหมือนโปรแกรมที่ 15 ส่ิงที่นาสนใจในโปรแกรมนี้คือ เงื่อนใขใน คําส่ัง for (ลูป i) จะไมเปนจริงเมื่อ *country_capital[i][0] มีคา เปน String null ซ่ึงไดกําหนด ปดทายคาตางๆ ที่กําหนดให กับ Array ของ Pointer *country_capital[i][0] จะใหคาตัวอักษรตัวแรก ของ String เชน ในรอบแรก (i = 0, j = 0) *country_capital[i][0] จะมีเปน T ในขณะที่ county_capital[i][j] มีคาเปน Thailand

9.6 Pointer ของ Pointer

เทาที่กลาวมา ตั้งแตตนของเรื่องนี้ จะกลาวถึงตัวแปร Pointer ซ่ึงเก็บคา Address ของตัวแปร ตัวแปร Pointer อาจจะเก็บคา Address ของตัวแปร Pointer อ่ืน ซ่ึงเก็บคา Address ของตัวแปร Pointer หรือ Pointer ตัวที่ 1 ช้ีไปที่ Address ของ Pointer ตัวที่ 2 ซ่ึง Pointer ตัวที่ 2 นี้ช้ีไปที่ Address ของ ตัวแปร ดังรูปที่ 1

รูปประกอบ ที ่1 Pointer ของ Pointer

322 112 Introduction to Programming ผศ.อุรฉัตร โคแกว 27

Page 28: บทที่ 9 Pointer · Pointer บทที่ 9 Pointer ใน C++ มีตัีวแปรอกชนิดหนึ่ างเร Pointer ียกวตัวแปร

Pointer

ในการกําหนด Pointer ของ Pointer จะตองเพิ่มเครื่องหมาย * อีก 1 ตัว หนา Pointer เชน

int **pt;

pt เปนตัวแปร Pointer ซ่ึงเก็บคา Address ของตัวแปร Pointer โดยตัวแปร Pointer ของตัวหลังนี้ จะเก็บ Address ของตัวแปรชนิดเลขจํานวนเต็ม

ในการกําหนดคาใหกับตัวแปร ซ่ึงถูกชี้โดย Pointer ในลักษณะดังกลาวจะตองเพิ่มเครื่องหมาย * อีกตัวหนาตัวแปร Pointer ซ่ึงอยูทางดานซายของเครื่องหมาย =

ในโปรแกรมที่ 20 pt1 จะเก็บคา Address ของตัวแปร pointer pt2 และตัวแปร Pointer pt2 จะเก็บ Address ของตัวแปร nu ในตอนเริ่มแรก nu มีคาเปน 20 คําสั่งกําหนดคาในบรรทัดที่ 9 จะทําใหตัวแปร nu มีคาเปน 40

ตัวอยาง โปรแกรมที่ 20 #include "stdio.h"

main()

{

int **pt1,*pt2,nu;

nu = 20;

pt2 = &nu;

pt1 = &pt2;

**pt1 = 40;

printf("nu = %d\n",**pt1);

}

322 112 Introduction to Programming ผศ.อุรฉัตร โคแกว 28

Page 29: บทที่ 9 Pointer · Pointer บทที่ 9 Pointer ใน C++ มีตัีวแปรอกชนิดหนึ่ างเร Pointer ียกวตัวแปร

Pointer

ผลลัพธ nu = 40 ตัวอยาง โปรแกรมที่ 21 #include "stdio.h"

main()

{

char **spt1,*spt2,str[20];

spt2 = str;

spt1 = &spt2;

printf("Enter strings : ");

gets(*spt1);

printf("%s\n",*spt1);

}

ผลลัพธ Enter strings : How do you do?

How do you do?

โปรแกรมที่ 21 เปนการใช Pointer ของ Pointer กับขอมูล String ใน Function gets() และ printf() จะตองมีเครื่องหมาย * หนา Pointer spt1 ในที่นี้ *spt1 คือ Pointer ซ่ึงชี้ไปที่ Address ของ

322 112 Introduction to Programming ผศ.อุรฉัตร โคแกว 29

Page 30: บทที่ 9 Pointer · Pointer บทที่ 9 Pointer ใน C++ มีตัีวแปรอกชนิดหนึ่ างเร Pointer ียกวตัวแปร

Pointer

Array string str สําหรับ Function gets() จะสงตัวอักษรที่ปอนทางแปนพิมพ ไปเก็บใน Array ซ่ึงถูกช้ีโดย Pointer *spt1 หรือ spt2 ทํานองเดียวกัน Function printf() จะนําขอมูล ซ่ึงอยูใน Array ซ่ึงถูกช้ีโดย Pointer *spt1 หรือ spt2 มาแสดงบนจอภาพ

9.7 Pointer กับ ขอมูลชนิด Structure

เราสามารถใช pointer กับขอมูลชนิดโครงสรางได โดยใช operator เครื่องหมาย -> ตามหลัง pointer แลวตามดวยช่ือ field ใน structure เพื่ออางอิงถึงขอมูลในแตละ field ได ดังตัวอยาง

struct STUDENT //สราง structure ช่ือ STUDENT

{ char code[8];

char name[25];

int midterm;

int final;

char grade;

} STD; //ประกาศตัวแปร STD เปนชนิด structure ที่ช่ือ STUDENT

STUDENT *ptr; //ประกาศตัวแปร pointer ช่ือ ptr เปนชนิดเก็บ address ของ //structure

ptr = &STD; //ให pointer ptr เก็บ address ของตัวแปร STD ซ่ึงเปนชนิด //structure

// การอางอิงถึงขอมูลใน structure โดยใช pointer มีรูปแบบ คือ pointer->field_name ดัง ตัวอยาง

strcpy(ptr->code, "1000105");

strcpy(ptr->name,"Urachart Kokaew");

ptr->midterm=30; ptr->final=50;

ptr->grade='A'; cin>>ptr->code; //รับขอมูลทางแปนพิมพ

322 112 Introduction to Programming ผศ.อุรฉัตร โคแกว 30

Page 31: บทที่ 9 Pointer · Pointer บทที่ 9 Pointer ใน C++ มีตัีวแปรอกชนิดหนึ่ างเร Pointer ียกวตัวแปร

Pointer

cin>>ptr->midterm; //รับขอมูลทางแปนพิมพ

cout<<ptr->code; //แสดงขอมูล cout<<ptr->midterm; //แสดงขอมูล

ตัวอยางโปรแกรมที่ 22 แสดงการใช array จัดเก็บ pointer เพื่ออางอิงถึงขอมูลใน structure

#include <iostream.h>

#include <conio.h>

#include <stdio.h>

#include <string.h>

void Input(); //declaration prototype functions in program

void Display();

struct mystruct //declare structure

{ int number;

char name[21];

double age;

};

mystruct s[3]; //declare variable array of structure 3 elements

mystruct *sptr[3]; //declare array of pointer 3 elements

void main() { clrscr(); Input();

Display(); getch();

}

322 112 Introduction to Programming ผศ.อุรฉัตร โคแกว 31

Page 32: บทที่ 9 Pointer · Pointer บทที่ 9 Pointer ใน C++ มีตัีวแปรอกชนิดหนึ่ างเร Pointer ียกวตัวแปร

Pointer

void Input() // input data to array by pointer

{ cout<<endl<<"Input data to structure by pointer 3 Item: \a"<<endl;

for(int i=0;i<=2;i++)

{ sptr[i]=&s[i]; //pointer point at each array of structure

cout<<"Code: ";cin>>sptr[i]->number;

cout<<"Name: ";cin>>sptr[i]->name;

cout<<"Age: ";cin>>sptr[i]->age;

cout<<endl;

}

}

void Display() //display all data from array by using pointer sptr[i]

{ cout<<endl<<"Display data from structure by use pointer ->\a"<<endl;

for(int i=0;i<=2;i++)

{ cout<<sptr[i]->number<<endl; //use pointer

cout<<sptr[i]->name<<endl;

cout<<sptr[i]->age<<endl<<endl;

}

}

322 112 Introduction to Programming ผศ.อุรฉัตร โคแกว 32