structure programming...
DESCRIPTION
Structure Programming การเขียนโปรแกรมแบบโครงสร้าง. สัปดาห์ที่ 11 ตัว แปรชี้ตำแหน่ง (Pointer). objectives. เพื่อให้นิสิตรู้จักและเข้าใจ ตัวแปรชี้ตำแหน่ง ในภาษาซี สามารถ เขียนโปรแกรมภาษาซี โดย ใช้ตัวแปรชี้ตำแหน่งได้ - PowerPoint PPT PresentationTRANSCRIPT
Structure Programmingการเขี�ยนโปรแกรมแบบโครงสร�าง
ส�ปดาห์�ที่�� 11ตั�วแปรชี้��ตั�าแห์น�ง(Pointer)
objectives
เพื่ �อให์�น#ส#ตัร$�จั�กและเขี�าใจัตั�วแปรชี้��ตั�าแห์น�งในภาษาซี�
สามารถเขี�ยนโปรแกรมภาษาซี�โดยใชี้�ตั�วแปรชี้��ตั�าแห์น�งได�
สามารถน�าความร$�เร �องตั�วแปรชี้��ตั�าแห์น�งไปประย-กตั�เพื่ �อแก�ไขีโจัที่ย�ป.ญห์าในชี้�ว#ตัประจั�าว�นได�ได�อย�างถ$กตั�องเห์มาะสม
Outline
Pointer1
pp2
Array of Pointer & Array-Pointer RelateArray of Pointer & Array-Pointer Relate3
Pointer to Pointer & Pointer to StructurePointer to Pointer & Pointer to Structure4
Assignment 5
Arithmetic & Pointer Operator
Variable Declaration
เม �อม�การประกาศตั�วแปรใดๆ เชี้�นint number;
char letter;
คอมไพื่เลอร�จัะที่�าการจัองพื่ �นที่��ในห์น�วยความจั�าส�าห์ร�บเก2บค�าตั�วแปร number และค�าตั�วแปร letter สมม-ตั#ว�าเป3นตั�าแห์น�งห์น�วยความจั�าที่�� 10000 และ 10002 เม �อม�การก�าห์นดจัะที่�าการจัองห์น�วยความจั�าด�งน��number = 5;letter = ‘A’;
ที่��อย$�ขี�อ
ม$ล
10000
10001
10002
10003
10004
10005
10006
10007
5 ‘A’
number
letter
15
3.1415
'A'…
ที่��อยู่��ของขอมู�ล
(Address)
ขอมู�ล ประเภที่
0000
0002
0006
XXXX
int
float
char
ชื่��อตั�วแปร
ขนาด
count
pi
ch
2 bytes
4 bytes
1 bytes
…
0001
0003
0004
0005
……
Variable Declaration (cont.)
Size of Variable
ชี้น#ดขีองตั�วแปร ขีนาด ค�าตั��าส-ด ค�าส$งส-ดunsigned char 8
bit0 255
char 8 bit
-128 127
unsigned int16 bit 32 bit
00 65,535
4,294,967,295
short int16 bit
-32,768 32,767
int16 bit 32 bit
-32,768
-2,147,483,6
48
32,7672,147,483,6
47
unsigned long32 bit
04,294,967,2
95
long32 bit
-2,147,483,6
482,147,483,6
47
float32 bit 3.410-38
3.4 1038
double64 bit
1.7 10-308 1.710308
long double 80 bit
3.4 10-4932 3.4104932
1080long double
864double
432float
18unsigned char
18char
432unsigned long
432long
216unsigned int
216int
BytesBitsType
Size of Variable (cont.)
Basic Knowledge of Pointer
ภาษาซี�ม�ตั�วแปรที่��ม�ส�วนที่�าให์�โปรแกรมกระชี้�บย#�งขี4�นใน การที่�างาน ตั�วแปรชี้น#ดน��เร�ยกว�า “ ตั�วแปรชี้��ตั�าแห์น�ง
ห์ร อ พื่อยน�เตัอร� (Pointer)”
พื่อยน�เตัอร� ห์มายถ4ง ตั�วแปรใดๆ ที่��ใชี้�เก2บค�าขีองตั�าแห์น�งที่��อย$� (Address) ขีองตั�วแปรห์ร อขี�อม$ลเม �อเขี�าถ4งที่��อย$�โดยพื่อยน�เตัอร�ได�ก2จัะสามารถกระที่�าก�บขี�อม$ลได�ไม�ว�าจัะเป3นการน�าออกมาแสดงผลห์ร อกระที่�าก�บตั�วแปรอ �นๆตั�วแปรชี้น#ดพื่อยน�เตัอร�ม�ความเร2วในการที่�างานส$งตั�วแปรชี้น#ดพื่อยน�เตัอร�จัะเก2บค�าที่��อย$�ขีองห์น�วยความจั�าห์ล�ก ซี4�งตั�างก�บตั�วแปรปกตั#ที่��เก2บค�าที่��แที่�จัร#งขีองขี�อม$ล น��นค อการใชี้�ตั�วแปรชี้น#ดพื่อยน�เตัอร�จัะเป3นการเขี�าถ4งขี�อม$ลห์ร อเป3นการอ�างถ4งตั�าแห์น�งที่��เก2บขี�อม$ล
Pointer Declaration
ตั�วอยู่�างการใชื่ตั�วชื่��int * ptr_int; /* pointer to integer */float * ptr_float;// pointer to float char * ptr_char; /* pointer to char */
type *pointer_name
type คื�อ ชื่น�ดของตั�วแปรประเภที่ตั�วชื่�� (pointer)
* คื�อ เคืร��องหมูายู่แสดงว�าเป#นตั�วแปรประเภที่ตั�วชื่��pointer_name คื�อ ชื่��อของตั�วแปรประเภที่ตั�วชื่��
ตั�วแปรพื่อยน�เตัอร�ที่�าห์น�าที่��เก2บตั�าแห์น�งที่��อย$�ขีองตั�วแปรที่��ชี้�� น��นค อที่�าห์น�าที่��ชี้��ไปย�งตั�วแปรตั�วอ �น ด�งน��นการประกาศตั�วแปรพื่อยน�เตัอร� ตั�องสอดคล�องก�บชี้น#ดขีองตั�วแปรตั�วอ �น ที่��ตั�องการให์�ตั�วแปรพื่อยน�เตัอร�น��นจัะชี้��ไป เชี้�น
• char *prt;
• int *ip , *temp;
• double *dp;
ประกาศตั�วแปร prt ให์�เป3นตั�วแปรพื่อยน�เตัอร�ที่��ชี้��ไปย�ง ตั�วแปรชี้น#ด char
ประกาศตั�วแปร ip และ ตั�วแปร temp เป3นตั�วแปรพื่อยน�-เตัอร�ที่��ชี้��ไปย�งตั�วแปรชี้น#ด int
ประกาศตั�วแปร dp เป3นตั�วแปรพื่อยน�เตัอร�ที่��ชี้��ไปย�งตั�วแปรชี้น#ด double
Pointer Declaration (cont.)
Referencing Operator “&”
ตั�วด$าเน�นการ & ส�งกล�บเลขที่��อยู่�� (Address) ของว�ตัถุ'
ตั�วด$าเน�นการ & (Referencing Operator)
XXXX int15 count;
XXXX int *ptr;
ptr = &count; /* ptr มู�คื�าเป#น XXXX */
How the pointer work?
ตั�วด�าเน#นการอ�างถ4งที่��อย$� (Reference/Address operator) จัะใชี้�เคร �องห์มาย & น�าห์น�าตั�วแปร ซี4�งห์มายถ4งตั�าแห์น�งที่��อย$�ขีองตั�วแปรน��นในห์น�วยความจั�า
#include <stdio.h> void main(void){
int z = 10; int *ZPtr; ZPtr = &z;/*ตั�วแปรชี้น#ดตั�วชี้��เก2บเลขีที่��
อย$�ขีองตั�วแปร z*/ }
15
3.1415
0000
'A'…
ที่��อยู่��ของขอมู�ล
(Address)
ขอมู�ล ประเภที่
0000
0002
0006
XXXX
int
float
char
ชื่��อตั�วแปร
ขนาด
count
pi
ch
2 bytes
4 bytes
1 bytes
…
0001
0003
0004
0005
ptr = &count
ตั�วชื่�� ptr ชื่��ไปที่��อยู่��ของ
ตั�วแปรชื่��อ count
Referencing Operator “&” (cont.)
Referencing Operator “&” (cont.)
15
3.1415
0006
'A'
ที่��อยู่��ของขอมู�ล
(Address)
ขอมู�ล ประเภที่
0000
0002
0006
int
float
char
ชื่��อตั�วแปร
ขนาด
count
pi
ch
2 bytes
4 bytes
1 bytes
int *ptr_int = &count;
0002 float *ptr_float = π
0000
char *ptr_char = &ch;
Dereference Operator “*”
เป#น ตั�วด$าเน�นการเชื่�งออมู (Indirection) หร�อ กล�บ
การอางอ�ง (Dereferencing)ตั$าแหน�งที่��
XXXXint15 count;
XXXX int * ptr;
int count = 15, y, z[10]; int *ptr; /* ptr เป#นตั�วชื่�� int */
ptr = &count; /* ptr ชื่��ไปที่�� count */ y = *ptr; /* y มู�คื�า 15 */*ptr = 0; /* count มู�คื�า 0 */ ptr = &z[0]; /* ptr ชื่��ไปที่�� z[0] */
ตั�วด�าเน#นการอ�างถ4ง (Dereferencing
operator) ใชี้�เคร �องห์มาย * น�าห์น�าตั�วแปรพื่อยน�เตัอร� จัะเป3นการอ�างถ4งค�าขี�อม$ลที่��ตั�วแปรพื่อยน�เตัอร�น��นชี้��ไป ซี4�งค�าขี�อม$ลน��สามารถเปล��ยนแปลงค�าได�
#include <stdio.h>void main(void){
int x; int y = 20; int *z; z = &y; x = *z;/*เป3นการอ�างถ4งค�าขี�อม$ลที่��ตั�วแปรชี้น#ดตั�วชี้��*/ printf (“%d”, x)/*ได�ผลล�พื่ธ์�ค อ 20 */
}
Dereference Operator “*” (cont.)
ให์�ตั�วแปรพื่อยน�เตัอร� z ชี้��ไปย�งแอดเดรสขีองตั�วแปร y โดยการให์� z เก2บตั�าแห์น�งแอดเดรสขีอง y น��นเอง
How to show the pointer value?
ฟั.งก�ชี้�น printf สามารถแสดงตั�าแห์น�งที่��อย$� (address) ได�โดยใชี้�• เคร �องห์มาย %p เพื่ �อแสดงตั�าแห์น�งเป3นเลขีฐานส#บห์ก• เคร �องห์มาย %u เพื่ �อแสดงตั�าแห์น�งเป3นเลขีฐานส#บ
ผลล�พื่ธ์�ที่��ได�จัะอย$�ในร$ปแบบ XXXX:YYYY ห์ร อ XXXX ขี4�นอย$�ก�บ Memory Model ที่��ใชี้�
Example 1
D
0000
letter
19
0002
num
26.09
0004
point
????
pt_letter
????
pt_num
????
pt_point
0000 0002 0004
#include<stdio.h>#include<conio.h>int main(){ char letter = 'D'; int num = 19; float point = 26.09; char *pt_letter; int *pt_num; float *pt_point; pt_letter = &letter; pt_num = # pt_point = &point; printf("Address of letter = %p \n",pt_letter); printf("Address of num = %p \n",pt_num); printf("Address of point = %p \n",pt_point); return 0;}
Address of letter = 0000Address of num = 0002Address of point = 0004
D
0000
letter
19
0002
num
26.09
0004
point
0000
????
pt_letter
0002
????
pt_num
0004
????
pt_point
Example 1 (cont.)
Example 2
int main(){ int num1 = 113,num2; float price1 = 4.85; char hint1 = 'J',hint2; int *pt_num; float *pt_price; char *pt_hint; pt_num = &num1; pt_price = &price1; pt_hint = &hint1; num2 = *pt_num; hint2 = *pt_hint; printf ("Variable num1 = %d \n", num2); printf ("Variable price1 = %f \n",*pt_price); printf ("Variable hint2 = %c \n", hint2); return 0;}
113
--x--
num1
4.85
--y--
price1
J
--z--
hint1
--y--
pt_price
--x--
pt_num
--z--
pt_hint
num2 hint2
J113
Variable num1 = 113Variable price1 = 4.850000Variable hint2 = J
113
--x--
num1
4.85
--y--
price1
J
--z--
hint1
--y--
pt_price
--x--
pt_num
--z--
pt_hint
num2 hint2
J113
Example 2 (cont.)
The Other Format of Reference and Dereference Operators
a7FFF4 aPtrFFF4????
int a; /* a is an integer */ int *aPtr; /* aPtr is a pointer to an integer */ a = 7; aPtr = &a; /* aPtr set to address of a */
printf ("The address of a is %p\n" "The value of aPtr is %p\n\n", &a, aPtr);
The address of a is FFF4The value of aPtr is FFF4
printf ("The value of a is %d\n" "The value of *aPtr is %d\n\n", a, *aPtr);
The value of a is 7The value of *aPtr is 7
printf ("Proving that * and & are complements of " "each other.\n&*aPtr = %p\n*&aPtr = %p\n", &*aPtr, *&aPtr);
Proving that * and & are complements of each other.&*aPtr = FFF4*&aPtr = FFF4
The address of a is FFF4The value of aPtr is FFF4
The value of a is 7The value of *aPtr is 7
Proving that * and & are complements of each other.&*aPtr = FFF4*&aPtr = FFF4
Summary of Pointer Process
----
pt_name
----
variable2
การประกาศตั�วแปรพอยู่น+เตัอร+type *pt_name;
แสดงตั$าแหน�งขอมู�ลดวยู่ “&”
แสดงขอมู�ลดวยู่ “*”variable2 = *pt_name;
1906
variable1
d
1906
d
Outline
Pointer1
pp2
Array of Pointer & Array-Pointer RelateArray of Pointer & Array-Pointer Relate3
Pointer to Pointer & Pointer to StructurePointer to Pointer & Pointer to Structure4
Assignment 5
Arithmetic & Pointer Operator
Arithmetic & Pointer Operator
คณิ#ตัศาสตัร�ขีองตั�วแปรชี้��ตั�าแห์น�งสามารถที่�าได�เพื่�ยง การบวก การลบ การเพื่#�มค�าและการลดค�าเที่�าน��น ไม�สามารถกระที่�าการค$ณิ ห์าร ห์ร อการเล �อนบ#ที่ เคร �องห์มายที่างคณิ#ตัศาสตัร�ขีองพื่อยน�เตัอร�ได�แก�• + ห์มายถ4ง การบวก• – ห์มายถ4ง การลบ• ++ ห์มายถ4ง การเพื่#�มค�าคร��งละ 1• –- ห์มายถ4ง การลดค�าคร��งละ 1
เคร �องห์มาย “+” และ “– ใชี้�ได�” เฉพื่าะตั�วแปรชี้��ตั�าแห์น�งชี้น#ดเลขีจั�านวนเตั2มเที่�าน��นการบวกและลบตั�วแปรชี้��ตั�าแห์น�งค อ การเล �อนไปชี้��ในตั�าแห์น�งที่��อย$�ขีองตั�วแปรตัามที่��ก�าห์นด กรณิ�ที่��ตั�วแปร ip ชี้��ที่��ตั�าแห์น�ง 1000 เม �อที่�าค�าส��งตั�อไปน��
int *ip;
ip = &x;ip = ip + 9;
ค�าตั�วแปรพื่อยน�เตัอร� “ip” จัะเล �อนไปชี้��ที่��ตั�าแห์น�ง “1018”
Arithmetic & Pointer Operator (cont.)
การเพื่#�มค�าคร��งละ 1 ขีองตั�วแปรตั�วชี้��จัะเป3นการเล �อนตั�วชี้��พื่อยน�เตัอร�ตัามชี้น#ดขีองตั�วแปรที่��ที่�าการชี้�� เชี้�น พื่อยน�เตัอร� “ip” ชี้��ตั�วแปรประเภที่จั�านวนเตั2มที่��ตั�าแห์น�ง “1000” เม �อก�าห์นด
ip++;คอมไพื่เลอร�จัะที่�าการเล �อนตั�วชี้�� “ip” ไปย�งตั�าแห์น�ง “1002” เพื่ราะจั�านวนเตั2มเก2บ 2 ไบที่�
int *ip;char *cp;float *fp; 1000 1001 1002 1003 1004 1005 1006 1007
fp fp+1
ip+1 ip+2 ip
cp cp+1 cp+2 cp+3 cp+4 cp+5 cp+6 cp+7
ตั�าแห์น�งที่��อย$�
Arithmetic & Pointer Operator (cont.)
นอกจัากการกระที่�าที่างคณิ#ตัศาสตัร� ตั�วชี้��ย�งม�ความสามารถที่างตัรรกะ(เปร�ยบเที่�ยบก�นได�) เชี้�นเด�ยวก�นก�บตั�วแปรปกตั#
1. == ห์มายถ4ง ตั�าแห์น�งเด�ยวก�น2. != ห์มายถ4ง ตั�างตั�าแห์น�งก�น3. > ห์มายถ4ง ตั�าแห์น�งส$งกว�า4. >= ห์มายถ4ง ตั�าแห์น�งส$งกว�าห์ร อเที่�าก�น5. < ห์มายถ4ง ตั�าแห์น�งตั��ากว�า6. <= ห์มายถ4ง ตั�าแห์น�งตั��ากว�าห์ร อเที่�าก�น
เชี้�น &x[3] จัะน�อยกว�า &x[5] เป3นตั�น
Arithmetic & Pointer Operator (cont.)
Outline
Pointer1
pp2
Array of Pointer & Array-Pointer RelateArray of Pointer & Array-Pointer Relate3
Pointer to Pointer & Pointer to StructurePointer to Pointer & Pointer to Structure4
Assignment 5
Arithmetic & Pointer Operator
Pointer & Array
ตั�วชี้��และอาร�เรย�ม�ความส�มพื่�นธ์�ก�นอย�างมาก บ�อยคร��งม�การจั�ดการก�บสมาชี้#กขีองอะเรย�โดยใชี้�ตั�วแปรพื่อยน�เตัอร�เชี้�น ก�าห์นดให์� x[10] แล�วที่�าล�าด�บตั�อไปน��
ip = &x[0]; // ip = x;
y = *ip; // เป3นการน�าค�าที่�� x[0] ส�งให์�ตั�วแปร y
ที่��ตั�าแห์น�งใดๆ สามารถอ�างได�โดยใชี้�พื่อยน�เตัอร�บวกตั�วชี้��ตั�าแห์น�ง
++ip; // (++ip)y = *ip; // เป3นการน�าค�าที่�� x[1] ส�งให์� ตั�วแปร y
Array-Pointer Relate
การน�าตั�วชี้��อ�างตั�าแห์น�งอาเรย�ตั�างๆ (Array Pointer)
X[0]
X[1]
X[2]
X[3]
X[4]
X[5]
ip
ip+1
ip+2
ip+3
ip+4
ip+5
พื่อยน�เตัอร�
ตั�าแห์น�งอาเรย�
X[]={1,2,3,4,5,6}
*(ip+4) = ?
v[0] v[1] v[2] v[3] v[4]
3000 3004 3008 300C 3010
ตั�วชี้��และแถวล�าด�บในภาษาซี�น��น ม�ความใกล�ชี้#ดก�นอย�างมาก การประกาศ
float v[5]
เป3นการก�าห์นดแถวล�าด�บ v ขีนาด 5 น��นค อกล-�มขีองว�ตัถ-ตั#ดก�น 5 ชี้#�นม�ชี้ �อว�า v[0], v[1], v[2], v[3], v[4]
Array-Pointer Relate (cont.)
Array-Pointer Relate (cont.)
การประกาศ float *vPtr และก�าห์นดให์� vPtr ชี้��ไปย�งตั�วแปรแถวล�าด�บ v สามารถที่�าได�สองว#ธ์�ค อว#ธ์�ที่�� 1
vPtr = v;
ว#ธ์�ที่�� 2vPtr = &v[0];
v[0] v[1] v[2] v[3] v[4]
3000 3004 3008 300C 3010
vPtr
Array-Pointer Relate (cont.)
การก�าห์นด x = *vPtr ค อการส�าเนาค�าใน v[0] มาย�ง x
v[0] v[1] v[2] v[3] v[4]
3000 3004 3008 300C 3010
vPtr
Moving Pointer Array with “+=“ and “-=“
เม �อบวกห์ร อลบจั�านวนเตั2มก�บตั�วชี้��แล�ว • ค�าขีองตั�วชี้��มู�ไดเพ��มูหร�อลดลงตัามูตั�วเลขจำ$านวนน��น• ค�าขีองตั�วชี้��เพื่#�มห์ร อลดตัามตั�วเลขีจั�านวนน��นค$ณิก�บขีนาด
ขีองว�ตัถ-ที่��ตั�วชี้��น��นชี้��อย$�• ขีนาด (ไบที่�) ขี4�นก�บประเภที่ขีองขี�อม$ลที่��ใชี้�ในว�ตัถ-น��น
• ตั�วอย�าง (ก�าห์นดขีนาดขีองว�ตัถ- ชี้น#ด float ค อ 4 ไบที่�)vPtr += 2; // vPtr = vPtr+(2*4)
// or vPtr = 3000+(2*4)
v[0] v[1] v[2] v[3] v[4]
3000 3004 3008 300C 3010
vPtr
• vPtr += 2; // vPtr = vPtr+(2*4)// or vPtr = 3000+(2*4)
• หล�งจำากคื$าส��งขางตันแลว vPtr จำะชื่��ไปที่�� v[2]
v[0] v[1] v[2] v[3] v[4]
3000 3004 3008 300C 3010
vPtr
v[0] v[1] v[2] v[3] v[4]
3000 3004 3008 300C 3010
vPtr
Moving Pointer Array with “+=“ and “-=“ (cont.)
• vPtr -= 2; // vPtr = vPtr-(2*4)// or vPtr = 3008-(2*4)
• หล�งจำากคื$าส��งขางตันแลว vPtr จำะชื่��ไปที่�� v[0]
v[0] v[1] v[2] v[3] v[4]
3000 3004 3008 3012 3016
vPtr
v[0] v[1] v[2] v[3] v[4]
3000 3004 3008 3012 3016
vPtr
Moving Pointer Array with “+=“ and “-=“ (cont.)
v[0] v[1] v[2] v[3] v[4]
3000 3004 3008 300C 3010
vPtr vPtr+2 vPtr+4
vPtr+1 vPtr+3
Moving Pointer Array with “+“ and “-“
Calculation Element by Pointer
• vPtr = &v[0]; // vPtr = 3000• v2Ptr = &v[2]; // or v2Ptr = 3008• x = v2Ptr – vPtr; // x = ?
v[0] v[1] v[2] v[3] v[4]
3000 3004 3008 300C 3010
vPtr
v2Ptr
คื�าที่�� x ไดร�บคื�อจำ$านวนหน�วยู่ (Element) ของตั�วแปรแถุวล$าด�บน�บจำาก vPtr ถุ-ง vPtr2 ในกรณี�น��คื�อ 2
Using Pointer with Array
int num[5] = {12, 34, 112, 45, 907};int *pt_num;
12
0410
num[0]
34
0412
num[1]
112
0414
num[2]
45
0416
num[3]
907
0418
num[4]
pt_num = &num[1];
int temp;temp = *pt_num;
0350
pt_num
04120418
050A
temp
907
pt_num = &num[4];
type name[10];type *pt_name;pt_name = name; //pt_name = &name[0]
float num[] = {19.01, 26.09, -4.23, -4.24, -13.12}
19.01
0300
num[0]
26.09
0304
num[1]
-4.23
0308
num[2]
-4.24
030C
num[3]
-13.12
0310
num[4]
Using Pointer with Array (cont.)
float *pt_num;
19.01
0300
num[0]
26.09
0304
num[1]
-4.23
0308
num[2]
-4.24
030C
num[3]
-13.12
0310
num[4]
pt_num = num;
090D
pt_num
0300
float test1, test2;
032A
test1
322E
test2test1 = *(pt_num+3);
test2 = *(pt_num+1);
+3
-4.24
+1
26.09
The Other Using Pointer with Array
Array of Pointer
Type *Pointer_Array_Name[index];
type คื�อ ชื่น�ดของตั�วแปรประเภที่ตั�วชื่��ตัามูขอก$าหนด ของ Array
* คื�อ เคืร��องหมูายู่แสดงว�าเป#นตั�วแปรประเภที่ตั�วชื่��Pointer_Array_name คื�อ ชื่��อชื่'ดของตั�วแปรประเภที่ตั�วชื่��
index คื�อ ขนาดของอาเรยู่+ที่��จำองไวส$าหร�บเก/บชื่'ดของตั�ว แปรประเภที่ตั�วชื่��
Example 3
#include<stdio.h>void main(void){
int a, b, c, *pa[3];a=5; b=10; c=15;pa[0]=&a; pa[1]=&b; pa[2]=&c;printf(“\n pa 1=%d, 2=%d, 3=%d”,*pa[0],*pa[1],*pa[2]);
}
Array
int main(){ int i, offset, b[] = {10, 20, 30, 40}; int *bPtr = b; /* set bPtr to point to array b */
printf("Array b printed with:\n" "Array subscript notation\n"); for (i=0; i<=3; i++) printf("b[%d] = %d\n", i, b[i]); return 0;}
Array b printed with:Array subscript notationb[0] = 10b[1] = 20b[2] = 30b[3] = 40
Using Array in Pointer Format
int main(){ int i, offset, b[] = {10, 20, 30, 40}; int *bPtr = b; /* set bPtr to point to array b */
printf("Pointer/offset notation where\n" "the pointer is the array name\n"); for (offset = 0; offset<=3 ; offset++) printf("*(b + %d) = %d\n", offset, *(b + offset)); return 0;}
Pointer/offset notation whereThe pointer is the array name*(b + 0) = 10*(b + 1) = 20*(b + 2) = 30*(b + 3) = 40
int main(){ int i, offset, b[] = {10, 20, 30, 40}; int *bPtr = b; /* set bPtr to point to array b */
printf("Pointer subscript notation\n"); for (i=0 ; i<=3 ; i++) printf("bPtr[%d] = %d\n", i, bPtr[i]); return 0;}
Pointer subscript notationbPtr[0] = 10bPtr[1] = 20bPtr[2] = 30bPtr[3] = 40
Using Pointer in Array Format
Outline
Pointer1
pp2
Array of Pointer & Array-Pointer RelateArray of Pointer & Array-Pointer Relate3
Pointer to Pointer & Pointer to StructurePointer to Pointer & Pointer to Structure4
Assignment 5
Arithmetic & Pointer Operator
Pointer to Pointer
type **ptt_name;
type คื�อ ชื่น�ดของตั�วแปรประเภที่ตั�วชื่��* * คื�อ เคืร��องหมูายู่แสดงว�าเป#น
ตั�วแปรประเภที่ตั�วชื่�� ซ้อนตั�วชื่��ptt_name คื�อ ชื่��อตั�วแปรประเภที่ตั�วชื่��ซ้อนตั�วชื่��
Pointer to Pointer (cont.)
float time = 9.28;
0100
time
9.28float *pt_time;
float **ptt_time;0250
pt_time
03AA
ptt_time
0250
pt_time = &time;
ptt_time = &pt_time;
0100
float temp1;
float temp2;
temp1 = *pt_time;
temp2 = **ptt_time; 0510
temp1
9.28
05C0
temp2
9.28
Example 4
#include<stdio.h> int main(void){ char **ptr = NULL; char *p = NULL; char c = 'd'; p = &c; ptr = &p; printf("\n c = [%c]\n",c); printf("\n *p = [%c]\n",*p); printf("\n **ptr = [%c]\n",**ptr); return 0;}
Structure & Pointer
struct student{ char name[40]; int age ; };struct student s;struct student *sPtr;strcpy(s.name,“Somsak”);s.age = 19; sPtr = &s;
ห์ากตั�องการพื่#มพื่�ว�าชี้ �อน�กศ4กษา สามารถที่�าได�สองว#ธ์�ค อ
• printf("%s", s.name);• printf("%s", sPtr->name);
Somsak\0 19s sPtr
xxxx
xxxx
s.name s.age
struct card { char *face; char *suit;};struct card a;struct card *aPtr;
ห์ากตั�องการพื่#มพื่�ว�าไพื่�ใบน��อย$�ในชี้-ดไห์น สามารถที่�าได�สองว#ธ์�ค อ
• printf("%s", a.suit);• printf("%s", aPtr->suit);
a aPtrxxxx
a.suit
heart\0nnnn
a.face
Queen\0mmmm
a.face = “Queen”;a.suit = “heart”;
aPtr = &a;
mmmm nnnn xxxx
Structure & Pointer (cont.)
Example 5
int main(){ struct card { char *face; // 2,3,..,9,J,Q,K,A char *suit; // space, heart, diamond, club }; struct card a; struct card *aPtr; a.face = “Ace"; a.suit = “spade"; aPtr = &a; printf( "%s%s%s\n%s%s%s\n%s%s%s\n",
a.face , " of " ,a.suit, aPtr->face , " of " , aPtr->suit,
(*aPtr).face, " of " , (*aPtr).suit ); return 0;}
Ace of spadeAce of spadeAce of spade
mmmm nnnna a.face a.suit
aPtrxxxx
xxxx yyyy
yyyy
Ace\0 space\0nnnnmmmm
Example 6
#include <stdio.h>#include <conio.h> int main() { struct st { int id; char *name; char *address; }; struct st employee, *stptr; stptr = &employee; stptr->id = 1; stptr->name = "Angelina"; stptr->address ="Rohini,Delhi"; printf("Employee Information: id=%d\n%s\n%s\n", stptr->id, stptr->name,stptr->address); printf("Employee Information: id=%d\n%s\n%s\n", employee.id, employee.name, employee.address); return 0; }
Example 7
จังเขี�ยนโปรแกรมเก2บขี�อม$ลน�กศ4กษาจั�านวน 10 คน โดยม�รายละเอ�ยดด�งน��• ขี�อม$ลประกอบด�วย ชี้ �อก�บ อาย-• ร�บขี�อม$ลน�กศ4กษาจัากค�ย�บอร�ด• โปรแกรมจัะตั�องใชี้�ตั�วชี้��ที่��ชี้��ไปย�งขี�อม$ล
ประเภที่โครงสร�าง• เม �อป;อนขี�อม$ลเสร2จั โปรแกรมจัะค�นห์า
น�กเร�ยนที่��อาย-มากกว�า 20 ป< แสดงชี้ �อ ออกจัอภาพื่
Output
Student[0] name:joy age:12Student[1] name:boy age:20Student[2] name:jo age:23Student[3] name:pat age:21Student[4] name:ple age:13Student[5] name:tom age:11
Student[6] name:tu age:25Student[7] name:tee age:34Student[8] name:bat age:44Student[9] name:phon age:33
jo,23 pat,21 tu,25 tee,34 bat,44 phon,33
#include<stdio.h>#include<conio.h>
int main(){ struct profile{ char name[20]; int age;
} s[10];
int i;
struct profile *sPtr;
sPtr = s;
Example 7 (cont.)
for (i=0; i<10; i++) { printf("Student # %d\n\tName :", i+1 ); scanf("%s",sPtr->name); printf("\tAge:"); scanf("%d",&(sPtr->age)); sPtr++; } sPtr -= 10; for (i=0; i<10; i++) { if ((sPtr->age) > 20) printf("\n%s, %d",sPtr->name,sPtr->age); sPtr++; }
return 0;}
Example 7 (cont.)
#include<stdio.h>typedef struct telephone{
char *name;int number;
}TELEPHONE;
int main() {
TELEPHONE index;TELEPHONE *ptr_myindex;
ptr_myindex = &index;
ptr_myindex->name = "Jane Doe";ptr_myindex->number = 12345;printf("Name: %s\n", ptr_myindex->name);printf("Telephone number: %d\n", ptr_myindex->number);
return 0;}
Example 7 (Pointer Form)
#include<stdio.h>typedef struct telephone{
char *name;int number;
}TELEPHONE;
int main() {
TELEPHONE index;TELEPHONE *ptr_myindex;
ptr_myindex = &index;
ptr_myindex->name = "Jane Doe";ptr_myindex->number = 12345;printf("Name: %s\n", ptr_myindex->name);printf("Telephone number: %d\n", ptr_myindex->number);
return 0;}
Example 7 (Pointer Form)
Dynamic Memory Allocation
เป3นการร�องขีอพื่ �นที่��ห์น�วยความจั�าในขีณิะประมวลผล กล�าวค อ เม �อโปรแกรมก�าล�งที่�างาน เม �อตั�องการใชี้�พื่ �นที่��ในห์น�วยความจั�าจั�านวนมากน�อยเพื่�ยงใด สามารถร�องขีอได�ค�าส��งเบ �องตั�นที่��เก��ยวขี�อง (Libray ชี้ �อ <stdlib.h>)
void* malloc(size_of_memory)malloc - เป3นฟั.งก�ชี้�นในการร�องขีอห์น�วยความจั�าแบบ dynamic โดยห์ากสามารถจัองพื่ �นที่��ในห์น�วยความจั�าได� จัะได�ค�าส�งกล�บมาเป3น ตั�าแห์น�งขีองห์น�วยความจั�า (void*) แตั�ถ�าไม�สามารถจัองได�จัะให์�ค�าเป3น NULL กล�บค นมาsize_of_memory –ขีนาดขีองห์น�วยความจั�าที่��ตั�องการเป3นจั�านวนไบตั�
*NULL เป3นค�าขีองตั�าแห์น�งว�าง
Dynamic Memory Allocation
ค�าส��งเบ �องตั�นที่��เก��ยวขี�องvoid free(ptr)
free – เป3นฟั.งก�ชี้�นในการค นห์น�วยความจั�าที่��ไม�ได�ใชี้�งานptr – ตั�าแห์น�งขีองห์น�วยความจั�าที่��จัะค น
Example
Example
#include <stdio.h> #include <stdlib.h> int main(void) {
int i; int* i_array; // this will be the array int j;
// find out how many integers are required printf("How many integers? "); scanf("%d",&i);
// Now allocate memory space i_array = (int*)malloc(i*sizeof(int)); // allocate storage for the array
// code that uses the new array for (j=0;j<i;j++) { i_array[j]=j; // the pointer can be used as an array
printf("%d\n",i_array[j]); }
free((void*) i_array); // free up memory for reuse.system("pause");return 0;
}
One-dimensional arrays
Multi-dimensional arrays
#include <stdio.h>#include <stdlib.h>
int main(void) {
int i,nrows,ncols;
// Define the size of the array at run time printf("Enter number of rows and number of columns ");
scanf("%d %d",&nrows,&ncols);
int** array; array=(int**)malloc(nrows*sizeof(int*));
for (i=0; i<nrows; i++) array[i]=(int*)malloc(ncols*sizeof(int));
func(array, nrows, ncols); // some function that uses the array ....
for (i=0; i<nrows; i++) free((void*)array[i]);
free((void*)array); system("pause"); return 0;
}
Outline
Pointer1
pp2
Array of Pointer & Array-Pointer RelateArray of Pointer & Array-Pointer Relate3
Pointer to Pointer & Pointer to StructurePointer to Pointer & Pointer to Structure4
Assignment 5
Arithmetic & Pointer Operator
Assignment (Class)
ให์�น#ส#ตัเขี�ยนโปรแกรมเพื่ �อที่�าการเก2บขี�อม$ลโดยใชี้�ว#ธ์�การจัองห์น�วยความจั�าแบบ Dynamic และเชี้ �อมตั�อขี�อม$ลก�นไปเร �อยๆ โดยม�ขี�อก�าห์นดด�งน�� 1. ม�เมน$ส�าห์ร�บการจัองพื่ �นที่��ในการเก2บขี�อม$ลส�าห์ร�บ node ให์ม� ซี4�งเม �อเล อกแล�วจัะรอร�บขี�อม$ลที่��จัะเก2บ 2. เชี้ �อมขี�อม$ลตัามล�าด�บ node ที่��ป;อนขี�อม$ลเขี�ามา 3. ม�เมน$ส�าห์ร�บการแสดงผลขี�อม$ล โดยแสดงตั�าแห์น�งป.จัจั-บ�น ขี�อม$ลในตั�าแห์น�งน��น และ ตั�าแห์น�งถ�ดไปขีองขี�อม$ล 4. ม�เมน$ออกจัากโปรแกรม โดยน#ส#ตัจัะตั�องที่�าการค นห์น�วยความจั�ากล�บค นก�อนที่��จัะออกจัากโปรแกรมด�วย