240-101 Introduction to Computer Programming

31
240-101 Introduction to Computer Programming 1 240-101 240-101 Introduction to Introduction to Computer Programming Computer Programming Email: [email protected]. Department of Computer Engineering Faculty of Engineering Prince of Songkla University คคคคคคคคคคคคคคค คคคคคคคคคคคคคคคคคคค

description

Department of Computer Engineering Faculty of Engineering Prince of Songkla University. 240-101 Introduction to Computer Programming. Email: [email protected]. คณาจารย์ภาควิชาวิศวกรรมคอมพิวเตอร์. Department of Computer Engineering Faculty of Engineering - PowerPoint PPT Presentation

Transcript of 240-101 Introduction to Computer Programming

Page 1: 240-101 Introduction to Computer Programming

240-101 Introduction to Computer Programming1

240-101240-101Introduction to Introduction to

Computer Computer ProgrammingProgramming

Email: [email protected].

th

Department of Computer Engineering Faculty of Engineering Prince of Songkla University

คณาจารยภาควชาวศวกรรมคอมพวเตอร

Page 2: 240-101 Introduction to Computer Programming

240-101 Introduction to Computer Programming2

บทท บทท 66พอยนเตอร พอยนเตอร (Pointer)(Pointer)

Department of Computer Engineering Faculty of Engineering Prince of Songkla University

Page 3: 240-101 Introduction to Computer Programming

240-101 Introduction to Computer Programming3

หวขอศกษา พอยนเตอรคออะไร สญญลกษณทใชกบพอยนเตอร การเรยกฟงกชนแบบ Call-by-

Reference การกระทำาทางคณตศาสตรกบพอยนเตอร การจองหนวยความจำา อารเรยของพอยนเตอร

Page 4: 240-101 Introduction to Computer Programming

240-101 Introduction to Computer Programming4

พอยนเตอรคออะไร เมอมการประกาศตวแปร คาของตวแปรจะถกเกบอยใน

หนวยความจำา

แอดเดรสของตวแปร คอ ตำาแหนงในหนวยความจำาทใชเกบตวแปรตวนน เขยนแทนดวยเครองหมาย &– เชน แอดเดรสของตวแปร x เขยนแทนดวย &x

560000

1055900

y

x

...

...

int x = 10;...

int y = 5;

Page 5: 240-101 Introduction to Computer Programming

240-101 Introduction to Computer Programming5

พอยนเตอรคออะไร (ตอ) พอยนเตอร คอ ตวแปรชนดหนงทใชในการเกบคา

แอดเดรสของตวแปรตวอน พอยนเตอรมหลายชนด(ตามชนดตวแปรทมอย)

– พอยนเตอรของตวแปร int– พอยนเตอรของตวแปร float– พอยนเตอรของตวแปร char– ฯลฯ

ประโยชนของพอยนเตอร– ใชในการ พฒนาโครงสรางขอมล (Data

Structure)– ใชในโปรแกรมทตองมการตดตอกบหนวยความจำา

โดยตรง

Page 6: 240-101 Introduction to Computer Programming

240-101 Introduction to Computer Programming6

สญลกษณทใชกบพอยนเตอร การประกาศตวแปร ใชเครองหมาย * เชน

– int *countPtr; countPtr เปนพอยนเตอรของ int (ซงสามารถใชในการเกบคาแอดเดรสของตวแปร int ได)

– char *str;

str เปนพอยนเตอรของ char– float *nums;

nums เปนพอยนเตอรของ float

Page 7: 240-101 Introduction to Computer Programming

240-101 Introduction to Computer Programming7

สญลกษณทใชกบพอยนเตอร (ตอ) การกำาหนดคาใหกบพอยนเตอร

– ใชเครองหมาย =– คาทกำาหนดใหกบพอยนเตอรตองเปนคา

แอดเดรสของตวแปร

int num = 6;int *numPtr;numPtr = #

50000numPtr

6num

6numnumPtrกำาหนดให numPtr ช

ไปท num

5000040000

Page 8: 240-101 Introduction to Computer Programming

240-101 Introduction to Computer Programming8

สญลกษณทใชกบพอยนเตอร (ตอ) การอางองถงคาในตำาแหนงทพอยนเตอรช

อย– เรยกอกอยางวา “Dereferencing”– ใช เครองหมาย * นำาหนาตวแปร– ตวอยางเชน cout << *numPtr;*numPtr = 20;

6numnumPtr20

Page 9: 240-101 Introduction to Computer Programming

240-101 Introduction to Computer Programming9

2.5a

p

5.6b

q

ตวอยางท 6-1#include <iostream.h>int main(){ float a = 2.5,b = 5.6; float *p,*q; p = &a; q = &b; cout << "*p = " << *p << endl; cout << "*q = " << *q << endl; *p = *p + 1; *q = *q + 3; cout << "a = " << a << endl; cout << "b = " << b << endl; return 0;}

3.5 8.6

*p = 2.5*q = 5.6a = 3.5b = 8.6

ผลลพธ

Page 10: 240-101 Introduction to Computer Programming

240-101 Introduction to Computer Programming10

การเรยกใชแบบ Call-by-Reference

การเรยกใชฟงกชนม 2 แบบ คอ Call-by-Value และ Call-by-Reference

Call-by-Value– ใชวธการสงคาของตวแปรใหกบฟงกชน– ไมสามารถแกไขคาของอารกวเมนตภายในฟงกชนได– ใชกบฟงกชนทรบคาเขาเปนตวแปรธรรมดา (int, float,

char, ...) Call-by-Reference

– ใชวธการสงแอดเดรสของตวแปรไปใหฟงกชน– ใชกบฟงกชนทรบคาเขาเปนพอยนเตอรหรออารเรย

Page 11: 240-101 Introduction to Computer Programming

240-101 Introduction to Computer Programming11

ตวอยางท 6-2#include <iostream.h>int squareByValue(int n);int main(){ int number = 5; cout << "The original value of number is " << number <<endl; number = squareByValue(number); cout << "The new value of number is " << number << endl; return 0;}

int squareByValue(int n){ n = n*n; return n;}

5number

main

squareByValue

n525

25

Page 12: 240-101 Introduction to Computer Programming

240-101 Introduction to Computer Programming12

ตวอยางท 6-3#include <iostream.h>void squareByReference(int *nPtr);int main(){ int number = 5; cout << "The original value of number is " << number <<endl

; squareByReference(&number); cout << "The new value of number is " << number << endl; return 0;}

void squareByReference(int *nPtr){ *nPtr = *nPtr * *nPtr;}

nPtr

5number

main

squareByReference

25

Page 13: 240-101 Introduction to Computer Programming

240-101 Introduction to Computer Programming13

การใชตวดำาเนนทางคณตศาสตรกบพอยนเตอร

ตวดำาเนนการทางคณตศาสตรทมกจะใชกบพอยนเตอร– +, -, ++, --, +=, -=– ใชในการเลอนพอยนเตอรไปขางหนาหรอถอยหลง

การเลอนพอยนเตอรจะเลอนทละ 1 บลอก เชน– ถาเปนพอยนเตอรของ int 1 บลอกคอ 4 ไบต– ถาเปนพอยนเตอรของ char 1 บลอกคอ 1 ไบต– ถาเปนพอยนเตอรของ float 1 บลอกคอ 4 ไบต– ฯลฯ

Page 14: 240-101 Introduction to Computer Programming

240-101 Introduction to Computer Programming14

ตวอยางการเลอนพอยนเตอร

v[0]

vPtr

v[1] v[2] v[3] v[4]

3000 3004 3008 3012 3016int v[5];int *vPtr;vptr = v;//or vPtr = &v[0];

v[0]

vPtr

v[1] v[2] v[3] v[4]

3000 3004 3008 3012 3016

vPtr = vPtr + 2;

Page 15: 240-101 Introduction to Computer Programming

240-101 Introduction to Computer Programming15

ตวอยางท 6-4#include <iostream.h>#include <string.h>int main(){ char msg[10]; char *ptr; cout << "Enter text to reverse: "; cin >> msg; int len = strlen(msg);

ptr = &msg[len-1]; while(ptr >= &msg[0]) { cout << *ptr; ptr--; } return 0;}

Enter text to reverse: CoeeoC

'\0''e''o''C'msg[3]msg[2]msg[1]msg[0]

...msg[9]

ptr

Page 16: 240-101 Introduction to Computer Programming

240-101 Introduction to Computer Programming16

การใชคำาสง cout กบ char * และ อารเรยของ char ถาใชคำาสง cout กบตวแปรอารเรยของ char สง

ทพมพออกมาคอ ตวอกษรตงแตอลเมนตแรกไปจนกวาจะเจอ ‘\0’

ถาใชคำาสง cout กบพอยนเตอรชนด char * สงทพมพออกมาคอ ตวอกษรตงแตตำาแหนงทพอยนเตอรชอยไปจนกวาจะเจอ ‘\0’

ตวอยางเชนchar name[8] = "Somchai";cout << name; char *namePtr = name;cout << namePtr; namePtr = &name[3];

cout << namePtr;

Somchai

Somchai

chai

Page 17: 240-101 Introduction to Computer Programming

240-101 Introduction to Computer Programming17

ความสมพนธระหวางอารเรยกบพอยนเตอร

ตวแปรอารเรยและพอยนเตอรสามารถใชงานแทนกนไดในหลายโอกาส

ตวแปรอารเรยถอเปนพอยนเตอรเนองจากคาในตวแปรอารเรยคอแอดเดรสของอลเมนตแรก

Page 18: 240-101 Introduction to Computer Programming

240-101 Introduction to Computer Programming18

ความสมพนธระหวางอารเรยกบพอยนเตอร (ตอ)

สามารถกำาหนดใหพอยนเตอรชไปทอารเรยไดint b[5];int *bPtr;

bPtr = b; // มความหมายเหมอนกบ bPtr = &b[0]; สามารถใชสญลกษณ [] กบพอยนเตอรได เชน

– bPtr[2 ] หมายถง คาในตำาแหนงทถดจาก bPtr ชอย ไปอก 2 บลอก

– bPtr[0] หมายถง คาในตำาแหนงเดยวกบท bPtr ชอย

Page 19: 240-101 Introduction to Computer Programming

240-101 Introduction to Computer Programming19

ตวอยางท 6-5#include <iostream.h>int main(){ int b[5] = {2,7,5,4,6}; int *bPtr; bPtr = b; cout << "bPtr[0] = " << bPtr[0] << endl; cout << "bPtr[1] = " << bPtr[1] << endl; bPtr = &b[2]; bPtr[1] = 10; cout << "b[1] = " << b[1] << endl; cout << "b[3] = " << b[3] << endl; return 0;}

bPtr[0] = 2bPtr[1] = 7b[1] = 7b[3] = 10

4b[3]

5b[2]

7b[1]

2b[0]

6b[4]

bPtr

10

Page 20: 240-101 Introduction to Computer Programming

240-101 Introduction to Computer Programming20

การจองหนวยความจำา (Memory Allocation) ในการประกาศตวแปรแตละครง คอมไพเลอรจะจองหนวย

ความจำาชวคราว เพอใชเกบคาของตวแปรตวนน เชน– int a; จองหนวยความจำาขนาด 4 ไบต– char ch;จองหนวยความจำาขนาด 1 ไบต – int nums[10]; จองหนวยความจำาขนาด 40 ไบต – char name[20]; จองหนวยความจำาขนาด 20 ไบต

แตหนวยความจำาเหลานจะมขอบเขตการใชงานอยภายในเครองหมาย {} ทไดมประกาศตวแปรไวเทานน– เชน ตวแปรทประกาศไวในฟงกชน เมอออกจากฟงกชนแลว

หนวยความจำาสำาหรบตวแปรเหลานนกจะถกคนใหแกระบบ

Page 21: 240-101 Introduction to Computer Programming

240-101 Introduction to Computer Programming21

การจองหนวยความจำา (ตอ) ถาตองการหนวยความจำาเพอใชในการเกบ

ขอมล โดยสามารถทจะคนหนวยความจำาสวนนเมอไรกได จะตองจองหนวยความจำาโดยใชคำาสง new

การจองหนวยความจำาโดยใชคำาสง new ม 2 แบบ คอ

– การจองหนวยความจำาแบบหนงบลอก– การจองหนวยความจำาเพอใชงานเปนอารเรย

Page 22: 240-101 Introduction to Computer Programming

240-101 Introduction to Computer Programming22

การจองหนวยความจำาแบบหนงบลอก

ใชแทนการประกาศตวแปร 1 ตว รปแบบ:

ตวแปรพอยนเตอร = new ชนดตวแปร; เชน

int *ptr;

ptr = new int; การคนหนวยความจำา

delete ptr;

ptr

Page 23: 240-101 Introduction to Computer Programming

240-101 Introduction to Computer Programming23

การจองหนวยความจำาเพอใชงานเปนอารเรย รปแบบ:

ตวแปรพอยนเตอร = new ชนดตวแปร[ขนาดอารเรย];

ตวอยางเชนint *nums;nums = new int[5];

การใชงานจะใชแบบอารเรยหรอพอยนเตอรกได เชนnums[1] = 10; // same as *(num+1) = 10;

การคนหนวยความ ตองมเครองหมาย [] หนาตวแปรพอยนเตอรดวยdelete []nums;

10nums

Page 24: 240-101 Introduction to Computer Programming

240-101 Introduction to Computer Programming24

ตวอยางท 6-6#include <iostream.h>int main(){ int num_std; cout << "Enter number of students: "; cin >> num_std;

float *scores = new float[num_std];

int i; for(i=0;i<num_std;i++) { cout << "Enter score of student " << i+1 << ": "; cin >> scores[i]; }

Page 25: 240-101 Introduction to Computer Programming

240-101 Introduction to Computer Programming25

ตวอยางท - 66 (ตอ) cout << "\nThe scores are "; float total = 0; for(i=0;i<num_std;i++) { cout << scores[i] << " "; total = total + scores[i]; }

cout << "\nTotal scores of all students is "<< total;

delete []scores; return 0;}

Page 26: 240-101 Introduction to Computer Programming

240-101 Introduction to Computer Programming26

ตวอยางท - 66 (ตอ) จากโปรแกรม จะเหนวาสามารถจองหนวย

ความจำาไดตามทผใชกำาหนด แตถาใชตวแปรอารเรยธรรมดา จะไม

สามารถทำาอยางนไดเนองจากขนาดอารเรยตองเปนคาคงท จะกำาหนดเปนตวแปรไมได

Page 27: 240-101 Introduction to Computer Programming

240-101 Introduction to Computer Programming27

สตรง (String) สตรงม 3 ชนด คอ

– คาคงทประเภทขอความ เชน "Sanook"– อารเรยของ char – พอยนเตอรชนด char *

Page 28: 240-101 Introduction to Computer Programming

240-101 Introduction to Computer Programming28

สตรงแบบทใชอารเรยของ char ตวอยางเชน

char name[8] = "Sawat";

การเปลยนแปลงขอความในอารเรยของ char– หามใชเครองหมาย = (เชน name =

"Sombat"; ไมได)– ใชฟงกชน strcpy เชนstrcpy(name,"Sombat");

name[0]

'S'

name[1]

'a'

name[2]

'w'

name[3]

'a'

name[4]

't'

name[5]

'\0'

name[6] name[7]

Page 29: 240-101 Introduction to Computer Programming

240-101 Introduction to Computer Programming29

สตรงแบบทใชพอยนเตอรชนด char *

สามารถกำาหนดคาโดยใชเครองหมาย = ได– char *str;– str = "Hello";

‘H’

str

‘e’ ‘l’ ‘l’ ‘o’ ‘\0’

หนวยความจำาชวคราว

Page 30: 240-101 Introduction to Computer Programming

240-101 Introduction to Computer Programming30

สตรงแบบทใชพอยนเตอรชนด char * (ตอ)

สามารถเปลยนแปลงขอความโดยใชฟงกชน strcpy กได แตตองมการจองหนวยความจำาเพอใชในการเกบขอความทจะนำามาวางเสยกอน

ตวอยางเชนchar *str = new char[6];strcpy(str,"Hello");

Page 31: 240-101 Introduction to Computer Programming

240-101 Introduction to Computer Programming31

อารเรยของพอยนเตอร ใชในกรณทตองการใชงานพอยนเตอร

หลายๆ ตว ตวอยางเชน

char *name_arr[3];name_arr[0] = "Somchai";name_arr[1] = "Sombat";name_arr[2] = "Sawat";

‘S’name_arr[0] ‘o’ ‘m’ ‘c’ ‘h’ ‘\0’‘a’ ‘i’

‘S’ ‘o’ ‘m’ ‘b’ ‘a’ ‘t’ ‘\0’name_arr[1]

‘S’ ‘a’ ‘w’ ‘a’ ‘t’ ‘\0’name_arr[2]