พอยน์เตอร์ ( pointer)
DESCRIPTION
พอยน์เตอร์ ( Pointer). CSC-113 Structure Programming. เนื้อหา. Pointer คืออะไร การประกาศตัวแปร Pointer การส่งค่ากลับของ Function Pointer กับ Array Pointer กับ String Array ของ Pointer Pointer ของ Pointer. 2. 2. Pointer คืออะไร. - PowerPoint PPT PresentationTRANSCRIPT
CSC-113 Structure Programming
พอยน์�เตอร์� (Pointer)
2
เนื้��อหา
2
Pointer คื�ออะไร การประกาศตั�วแปร Pointer การส่�งคื�ากลั�บของ Function Pointer ก�บ Array Pointer ก�บ String Array ของ Pointer Pointer ของ Pointer
3
Pointer คื�ออะไร
3
Data type ชนื้�ดหนื้��งที่ �ข!อมู#ลัที่ �เก$บเป%นื้ตั&าแหนื้�งที่ �อยู่#� (Address) ของข!อมู#ลั หร�อ การนื้&าตั�วแปรพอยู่นื้)เตัอร)ไปช �ตั&าแหนื้�งที่ �อยู่#�ของตั�วแปรที่ �อยู่#�ในื้หนื้�วยู่คืวามูจำ&า
Ptr เป%นื้ตั�วแปรแบบ Pointer
N เป%นื้ตั�วแปรแบบ integer
1001
350
เก็�บอย �ใน์ต��แหน์�งที่�� 1001
1001
350
.
.
memory
1001
1002
1003 Ptr
N
เก็�บอย �ใน์ต��แหน์�งที่�� 1003
4
Pointer คื�ออะไร
4
เมู��อตัองการเร ยู่กใชคืาใดที่ �เก$บอยู่#ในื้หนื้วยู่คืวามูจำ&า ก$ส่ามูารถเขาถ�งคืานื้��นื้ โดยู่การใชพอยู่นื้)เตัอรอางอ�งถ�งตั&าแหนื้งนื้��นื้ ที่&าใหการเขาถ�งขอมู#ลัที่&าไดเร$วกวาตั�วแปรชนื้�ดปกตั� แบ�งเป%นื้ 2 ชนื้�ด
1. Direct Pointer Variable
2. Indirect Pointer Variable
5
1. Direct Pointer Variable
5
คื�อ ตั�วแปรที่ �เก$บตั&าแหนื้�งที่ �อยู่#�ของข!อมู#ลัภายู่ในื้หนื้�วยู่คืวามูจำ&าโดยู่ตัรง
Ptr เป%นื้ตั�วแปรแบบ Pointer
N เป%นื้ตั�วแปรแบบ integer
1001
350
เก็�บอย �ใน์ต��แหน์�งที่�� 1001
1001
350
.
.
memory
1001
1002
1003 Ptr
N
เก็�บอย �ใน์ต��แหน์�งที่�� 1003
mem. address
6
2. Indirect Pointer Variable
6
คื�อ ตั�วแปร Pointer ที่ �เก$บตั&าแหนื้�งที่ �อยู่#�ของข!อมู#ลัของตั�วแปรแบบ Pointer อ กตั�วหนื้��ง บางคืร��งเร ยู่ก Pointer to Pointer
250เก็�บอย �ใน์ต��แหน์�งที่�� 3000
77
Ptr1 เป%นื้ตั�วแปรแบบ Pointer
N เป%นื้ตั�วแปรแบบ integer
5000
3000
250
5000
.
.
memory
1000
.
.
.
3000
Ptr2
N
เก็�บอย �ใน์ต��แหน์�งที่�� 10005000
.
.
.
Ptr1
Ptr2 เป%นื้ตั�วแปรแบบ Pointer
3000เก็�บอย �ใน์ต��แหน์�งที่�� 5000
mem. address
1
2
8
ประโยู่ชนื้)ของ Pointer
8
ใช!ส่�ง Array แลัะ String จำาก Function หนื้��งไป Function หนื้��ง ใช!จำ�ดการก�บ Array ใช!ก�บ Data Structure ที่ �ซั�บซั!อนื้ เช�นื้ Link lists, Binary Tree เป%นื้ตั!นื้ ใช!ในื้การร�บคื�ากลั�บมูายู่�งฟั2งก)ช�นื้มูากกว�า 1 คื�า
9
เนื้��อหา
9
Pointer คื�ออะไร การประกาศตั�วแปร Pointer การส่�งคื�ากลั�บของ Function Pointer ก�บ Array Pointer ก�บ String Array ของ Pointer Pointer ของ Pointer
type *prt_name;หร�อ type *prt1_name, *prt2_name, …;
1010
การประกาศตั�วแปร Pointer
type ชน์�ดของข�อมู ลที่��ต�วแปร์ Pointer เก็�บต��แหน์�งที่��อย �ptr_name ช"�อของต�วแปร์ Pointer ต�องมู� * น์��หน์��เพ"�อบอก็ว��เป#น์ต�วแปร์ Pointer
ตั�วอยู่�าง
int *p, *a;
1111
ตั�วด&าเนื้�นื้การของพอยู่นื้)เตัอร
ตั�วด&าเนื้�นื้การเคืร��องหมูายู่ดอกจำ�นื้ที่ร * (Asterisk) ใชก�บตั�วแปรพ
อยู่นื้)เตัอร แลัะเพ��อแส่ดงผลัขอมู#ลัที่ �เก$บอยู่#ในื้ตั�วแปรพอยู่นื้)เตัอร) ตั�วด&าเนื้�นื้การ & (Ampersand) ใชเพ��อนื้&าคืาตั&าแหนื้�งที่ �อยู่#ของตั�วแปร ไปเก$บไวในื้ตั�วแปรพอยู่นื้)เตัอร แลัะใชในื้การแส่ดงคืาแอดเดรส่ของตั�วแปรพอยู่นื้)เตัอร
การก&าหนื้ดคื�าให!พอยู่นื้)เตัอร)char m=‘T’;char *pm;pm = &m;
1212
#include<stdio.h>
void main()
{
int num = 15;
printf(“Number is %d\n”,num);
printf(“Address of num is %u\n”,&num);
}
1313
#include<stdio.h>
void main()
{
int num = 15;
printf(“Number is %d\n”,num);
printf(“Address of num is %u\n”,&num);
printf(“Value of address %u is %d\n”,&num,*(&num));
}
1414
#include<stdio.h>
void main()
{
int num = 15;
int *address;
address = #
printf(“Number is %d\n”,num);
printf(“Address of num is %u\n”,address);
printf(“Value of address %u is %d\n”,address,
*address);
}
1515
#include<stdio.h>
void main()
{
int num = 15;
int *address;
address = #
printf(“Number is %d\n”,num);
printf(“Address of num is %u\n”,address);
printf(“Value of address %u is %d\n”,address, *address);
*address = 100;
printf(“Number is %d\n”,num);
printf(“Value of address %u is %d\n”,address, *address);
}
1616
#include <stdio.h>void main() { int *p,q; q = 100; p = &q; printf(“%d”,*p); }
ประกาศ p เป%นื้ตั�วแปรแบบ Pointer แลัะ q เป%นื้ตั�วแปรแบบ integer
?
ตั�วอยู่�างที่ � 1
1234
1
ช �ไปไหนื้ไมู�ร#!
? p q
อย �ใน์ต��แหน์�งที่�� 5000 (สมูมูต�)
อย �ใน์ต��แหน์�งที่�� 3000 (สมูมูต�)
1717
ก&าหนื้ดคื�าให! q
100
ตั�วอยู่�างที่ � 1
2
ช �ไปไหนื้ไมู�ร#!
? p q
อย �ใน์ต��แหน์�งที่�� 5000 (สมูมูต�)
อย �ใน์ต��แหน์�งที่�� 3000 (สมูมูต�)
#include <stdio.h>void main() { int *p,q; q = 100; p = &q; printf(“%d”,*p); }
1234
1818
นื้&า address ของ q มูาเก$บไว!ที่ � p (ก&าหนื้ดให! p ช �ยู่�ง q)
100
ตั�วอยู่�างที่ � 1
3
5000 p q
อย �ใน์ต��แหน์�งที่�� 5000 (สมูมูต�)
อย �ใน์ต��แหน์�งที่�� 3000 (สมูมูต�)
#include <stdio.h>void main() { int *p,q; q = 100; p = &q; printf(“%d”,*p); }
1234
1919
เอาคื�าที่ � pointer p ช �อยู่#�ไปแส่ดงผลั
100
ตั�วอยู่�างที่ � 1
4
5000 p q
อย �ใน์ต��แหน์�งที่�� 5000 (สมูมูต�)
อย �ใน์ต��แหน์�งที่�� 3000 (สมูมูต�)
#include <stdio.h>void main() { int *p,q; q = 100; p = &q; printf(“%d”,*p); }
1234
100
12
2020
ประกาศ p เป%นื้ตั�วแปรแบบ Pointer แลัะ q เป%นื้ตั�วแปรแบบ integer
?
ตั�วอยู่�างที่ � 2
123
4
1
ช �ไปไหนื้ไมู�ร#!
? p q
อย �ใน์ต��แหน์�งที่�� 5000 (สมูมูต�)
อย �ใน์ต��แหน์�งที่�� 3000 (สมูมูต�)
#include <stdio.h>void main() { int *p,q; p = &q; *p = 1000; //q= 1000; printf(“%d”,q); }
2121ตั�วอยู่�างที่ � 2
123
4
#include <stdio.h>void main() { int *p,q; p = &q; *p = 1000; //q= 1000; printf(“%d”,q); }
นื้&า address ของ q มูาเก$บไว!ที่ � p (ก&าหนื้ดให! p ช �ยู่�ง q)
?
2
5000 p q
อย �ใน์ต��แหน์�งที่�� 5000 (สมูมูต�)
อย �ใน์ต��แหน์�งที่�� 3000 (สมูมูต�)
2222
นื้&าคื�า 1000 เก$บในื้ address ที่ � p ช �อยู่#�
1000
ตั�วอยู่�างที่ � 2
3
5000 p q
อย �ใน์ต��แหน์�งที่�� 5000 (สมูมูต�)
อย �ใน์ต��แหน์�งที่�� 3000 (สมูมูต�)
123
4
#include <stdio.h>void main() { int *p,q; p = &q; *p = 1000; //q= 1000; printf(“%d”,q); }
1000
12
2323
เอาคื�าในื้ตั�วแปร q ไปแส่ดงผลั
1000
ตั�วอยู่�างที่ � 2
4
5000 p q
อย �ใน์ต��แหน์�งที่�� 5000 (สมูมูต�)
อย �ใน์ต��แหน์�งที่�� 3000 (สมูมูต�)
1000
123
4
#include <stdio.h>void main() { int *p,q; p = &q; *p = 1000; //q= 1000; printf(“%d”,q); }
1 2
2424
#include<stdio.h>
void main()
{
int *pt,a,b;
a = 20;
pt = &a;
b = *pt;
printf(“%d\n”,b);
}
20
2525
#include<stdio.h>
void main()
{
int *pt,a;
a = 20;
pt = &a;
printf(“Address pointed by pt store value of %d\n ”,*pt);
*pt = 10;
printf(“After indirect assignment a = %d\n”,a);
}
2626
#include <stdio.h>void main() { int a,b,c; int *p,*q,*r; a = 6; b = 2; p = &b; q = p; r = &c; p = &a; *q = 8; *r = *p; *r = a + *q + *&c;
printf(“%d %d %d\n”,a,b,c); printf(“%d %d %d”,*p,*q,*r); }
ตั�วอยู่�างที่ � 3
? ? ?
? ? ?a b c
p q r
? ? ?
6 2 ?a b c
p q r
2727
#include <stdio.h>void main() { int a,b,c; int *p,*q,*r; a = 6; b = 2; p = &b; q = p; r = &c; p = &a; *q = 8; *r = *p; *r = a + *q + *&c;
printf(“%d %d %d\n”,a,b,c); printf(“%d %d %d”,*p,*q,*r); }
ตั�วอยู่�างที่ � 3
? ? ?
6 2 ?a b c
p q r
? ? ?
6 8 ?a b c
p q r
2828
#include <stdio.h>void main() { int a,b,c; int *p,*q,*r; a = 6; b = 2; p = &b; q = p; r = &c; p = &a; *q = 8; *r = *p; *r = a + *q + *&c; printf(“%d %d %d\n”,a,b,c); printf(“%d %d %d”,*p,*q,*r); }
ตั�วอยู่�างที่ � 3
6 8 20a b c
p q r
6 8 6a b c
p q r
2929
#include <stdio.h>void main() { int a,b,c; int *p,*q,*r; a = 6; b = 2; p = &b; q = p; r = &c; p = &a; *q = 8; *r = *p; *r = a + *q + *&c;
printf(“%d %d %d\n”,a,b,c); printf(“%d %d %d”,*p,*q,*r); }
ตั�วอยู่�างที่ � 3
6 8 20a b c
p q r
6 8 206 8 20
3030
#include <stdio.h>void main() { int a = 15; int b,c; int *pa, *pb, *pc; pa = &a; a = *pa; b = *pa + 5; pb = &b; pc = &c; *pc = a + b; printf(“Data in a,b,c = %d %d %d\n”,a,b,c); printf(“Address of a,b,c = %u %u %u\n”,pa,pb,pc);}
3131
แบบฝึ5กห�ด 1. ประกาศตั�วแปร MySalary = 5000 //แบบ int
2. ประกาศตั�วแปรพอยู่นื้)เตัอร) PMySalary ช �ไปยู่�ง ข!อมู#ลัแบบ int
3. ก&าหนื้ดให! PMySalary ช �ไปยู่�ง MySalary4. พ�มูพ)ตั&าแหนื้�งในื้ Memory ของ MySalary
5. พ�มูพ)ตั&าแหนื้�งในื้ Memory ของ PMySalary; 6. พ�มูพ)คื�าของ MySalary 7. พ�มูพ)คื�าที่ � PMySalary ช �ไป
8. &(*PMySalary) คื�อ ?
• #include <stdio.h>void main() {
int MySalary = 5000; //1int *PMySalary; //2PMySalary = &MySalary; //3printf("%d\n",&MySalary); //4printf("%d\n",PMySalary); //5printf("%d\n",MySalary); //6printf("%d\n",*PMySalary); //7printf("%d\n",&(*PMySalary)); //8
}
33
เนื้��อหา
33
Pointer คื�ออะไร การประกาศตั�วแปร Pointer การส่�งคื�ากลั�บของ Function Pointer ก�บ Array Pointer ก�บ String Array ของ Pointer Pointer ของ Pointer
3434
การส่�งคื�ากลั�บของ Function
ปกตั�การส่�งคื�ากลั�บของ Function จำะมู เพ ยู่ง 1 คื�าเที่�านื้��นื้ โดยู่ใช!คื&าส่��ง Return แตั�ในื้กรณี ที่ �ตั!องการให!มู การส่�งคื�ากลั�บหลัายู่คื�า จำะตั!องใช!ตั�วแปร Pointer ช�วยู่ การ Pass คื�าไปยู่�ง Function
Pass by Value (การส่�งคื�าของข!อมู#ลัไปยู่�ง Function) Pass by Reference (การส่�งตั&าแหนื้�งของข!อมู#ลัไปยู่�ง Function)
3535
Pass by Value#include <stdio.h>void pass(int ,int); void main() { int p = 10, q = 15; printf(“Before : p = %d, q = %d\n”,p, q); pass(p, q); printf(“After : p = %d, q = %d\n”,p, q);}void pass(int p, int q) { p = 5; q = 20; printf(“In function : p = %d, q = %d\n”,p, q); }
10 15p q
10 15p q
copy คื�า
5 20p q
10 15p q
3636
Pass by Reference#include <stdio.h>void pass(int *,int *); void main() { int p = 10, q = 15; printf(“Before : p = %d, q = %d\n”,p, q); pass(&p, &q); printf(“After : p = %d, q = %d\n”,p, q);}void pass(int *p, int *q) { ++*p; *q = 20; printf(“In function : p = %d, q = %d\n”,*p, *q); }
p q
10 15p q
p q
11 20p q
ช �ไปยู่�ง
ตั�วอยู่�างที่ � 1
3737
Pass by Reference#include <stdio.h>void swap(int *,int *);
void main() { int p = 50, q = 100; printf(“Before : p = %d, q = %d\n”,p, q); swap(&p, &q); printf(“After : p = %d, q = %d\n”,p, q);}
void swap(int *pp, int *qq) { int tmp; tmp = *pp; *pp = *qq; *qq = tmp; pp = &q; printf(“In function : p = %d, q = %d\n”,*pp, *qq); }
pp qq
50 100p q
p q
ช �ไปยู่�ง
pp qq
50tmp
100 50
ตั�วอยู่�างที่ � 2
3838
Pass by Reference#include <stdio.h>void calcu(int *, int *, int *); void main() { int a = 3, b = 10, c= 5; calcu(&a, &b, &c); printf(“A = %d,B = %d,C = %d\n”,a,b,c);}void calcu(int *pa, int *pb, int *pc) { *pa = *pa + 5; *pb = *pb + 10; *pc = * pc + * pb;}
3a
ช �ไปยู่�ง
ตั�วอยู่�างที่ � 3
10b 5c
pa pb pc
8a
ช �ไปยู่�ง20b 25c
pa pb pc
39
เนื้��อหา
39
Pointer คื�ออะไร การประกาศตั�วแปร Pointer การส่�งคื�ากลั�บของ Function Pointer ก�บ Array Pointer ก�บ String Array ของ Pointer Pointer ของ Pointer
4040
Pointer ก�บ Array นื้&า Pointer มูาใช!ในื้การจำ�ดการข!อมู#ลั แลัะมูาใช!อ!างอ�งตั&าแหนื้�งที่ �อยู่#�ของแตั�ลัะตั�วแปรยู่�อยู่ของตั�วแปรช7ดได!เช�นื้ int number[] = {10,20,30,40,50};
อ!างอ�ง Address ของ Array : number + j หร�อ &number[j]= ตั&าแหนื้�งที่ �อยู่#�ของ number[j]
ข!อมู#ลัของ Array ตั�วที่ � j : number[j] หร�อ *(number + j)
4141
10
20
30
40
50
ข!อมู#ลัAddress
FFEC
FFEE
FFE0
FFF2
FFF4
number[0] หร�อ *(number+0)
number[1] หร�อ *(number+1)
number[2] หร�อ *(number+2)
number[3] หร�อ *(number+3)
number[4] หร�อ *(number+4)
&number[0] หร�อ number+0
&number[1] หร�อ number+1
&number[2] หร�อ number+2
&number[3] หร�อ number+3
&number[4] หร�อ number+4
4242
Pointer ก�บ Arraychar str[80], *pl;
pl = str;
หร�อ pl = &str[0];
การอ!างถ�งตั�วแปรช7ด ส่ามูารถอ!างถ�งได!โดยู่การเพ��มูหร�อลัดตั�วแปรพอยู่นื้)เตัอร)ได! เช�นื้ ถ!าตั!องการอ!างถ�งตั�วแปร str[4] โดยู่การอ!างถ�งตั&าแหนื้�งที่ �อยู่#� ส่ามูารถที่&าได!โดยู่การเพ��มูตั�วแปรพอยู่นื้)เตัอร) ด�งนื้ �
*(pl + 4)
str
pl
4343
#include <stdio.h>void printarray(int *); void main() { int array[ ] = {100, 200, 300, 400, 500}; printarray(array);}void printarray(int *ptr) { for (int i=0; i < 5; i++) printf(“Array#%d = %d\n”,i, ptr[i]); }
ตั�วอยู่�าง 1 : ส่�งคื�าตั&าแหนื้�งของตั�วแปรช7ดไปยู่�งฟั2งก)ช�นื้ส่&าหร�บแส่ดงข!อมู#ลั
Array#0 = 100Array#1 = 200Array#2 = 300Array#3 = 400Array#4 = 500
4444
#include <stdio.h>void printarray(int *); void main() { int array[ ] = {100, 200, 300, 400, 500}; printarray(array);}void printarray(int *ptr) { for (int i=0; i < 5; i++) printf(“Array#%d = %d\n”,i, *(ptr+i)); }
ตั�วอยู่�าง 1 : ส่�งคื�าตั&าแหนื้�งของตั�วแปรช7ดไปยู่�งฟั2งก)ช�นื้ส่&าหร�บแส่ดงข!อมู#ลั
Array#0 = 100Array#1 = 200Array#2 = 300Array#3 = 400Array#4 = 500
4545
#define SIZE 5#include <stdio.h>void inc_array(int *, int, int); void main() { int number[] = {100, 200, 300, 400, 500}; inc_array(number, SIZE, 5); //for (int i =0; i<SIZE;++i) // printf(“%d\n”, number[i]); //*(number+j)
}void inc_array(int *ptr, int s, int c) {
for (int j=0; j < s; j++){ *(ptr+j) += c; printf(“%d\n”,ptr[j]);}}
ตั�วอยู่�าง 2 : ส่�งคื�าตั&าแหนื้�งของตั�วแปรช7ดไปยู่�งฟั2งก)ช�นื้ส่&าหร�บแส่ดงข!อมู#ลั
105205305405505
4646
#include <stdio.h>void main() { int a[5] = {0,1,2,3,4}; int *ptr;
ptr = a;printf(“%d\n”,a[0]);printf(“%d\n”,*ptr);printf(“%d\n”,*(ptr+1));printf(“%d\n”,*(a+2));printf(“%d\n”,ptr[3]);
}
ตั�วอยู่�าง 3
4747
#include <stdio.h>void main() {
int number[3];int *pt;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));
}
ตั�วอยู่�าง 4
4848
#include <stdio.h>void main() {
int number[3];int *pt,i;number[0] = 200;number[1] = 400;number[2] = 600;pt = number;for( i = 0;i <= 2;++i)
printf(“number[%d] = %d\n”,i, pt[i]);}
ตั�วอยู่�าง 5
4949
#include <stdio.h>int array[]={4,5,8,9,8,1,0,1,9,3};int *array_ptr;void main() {
array_ptr = array;while(*array_ptr !=0)
++ array_ptr;printf(“Number of elements before zero %d\n”,
array_ptr-array);}
ตั�วอยู่�าง 6
5050
#include <stdio.h>#include <stdlib.h>void main() {
char sentence[10];char *Ptr;int count;Ptr = sentence;for(count = 0; count <10; count++){
*Ptr = getchar();++Ptr;
}for(count = 0; count <10; count++){
--Ptr;putchar(*Ptr);
}}
ตั�วอยู่�าง 7
5151
#include <stdio.h>long SumArray(int *tbl, int ct);void main() {
int Y[5]={5,7,9,11,13};long s;s = SumArray(Y,5);printf("Sum is %d", s);
}long SumArray(int *tbl, int ct){
int i;long sum = 0;for (i=0; i<ct; i++){
sum += *tbl;tbl++;
}return sum;
}
ตั�วอยู่�าง 8
52
เนื้��อหา
52
Pointer คื�ออะไร การประกาศตั�วแปร Pointer การส่�งคื�ากลั�บของ Function Pointer ก�บ Array Pointer ก�บ String Array ของ Pointer Pointer ของ Pointer
5353
Pointer ก�บ String
เช�นื้ char *ptr = “HELLO”; เที่�าก�บ char array[6] = “HELLO” H
ELL0\0
ptr1500
ข!อมู#ลัAddress
1500
1501
1502
1503
1504
1506
5454
#include <stdio.h>void main() { char *s; printf(“Enter String : ”); gets(s);// s = “dddd”; printf(s); // printf(“%s\n”,s);}
5555
#include <stdio.h>#include <conio.h>#include <string.h>void main() { char string[80], *pstr; printf("Enter String : "); gets(string); pstr = string; //pstr = &string[0] for (int i = strlen(string);i > 0; --i,pstr++)
printf("%c at %d\n",*pstr, pstr); }
5656
#include <stdio.h>void main() { char str[20] = “Hello girl”; char *spt; int i; spt = str;
for (i =0;spt[i];++i)printf(“%c”,spt[i]);
}
#include <stdio.h>void main() { char str[20] = “Hello boy”; char *spt;
spt = str;while(*spt){
printf(“%c”,*spt);spt++;
}}
5757
#include<stdio.h>
void main(){
char *ptr[2] = {“read error”,”write error”};
printf(“%s\n”,ptr[0]);
}
5858
#include<stdio.h>void strcopy(char *s1, char *s2){
while (*s1 != '\0'){
*s2 = *s1;s1++;s2++;
}*s2 = '\0';
}void main(){
char str1[20], str2[20];printf("This program will copy your string\n");scanf ("%s", str1);strcopy(str1, str2);printf("The original string is %s\n", str1);printf("The new copied string is %s\n", str2);
}
59
เนื้��อหา
59
Pointer คื�ออะไร การประกาศตั�วแปร Pointer การส่�งคื�ากลั�บของ Function Pointer ก�บ Array Pointer ก�บ String Array ของ Pointer Pointer ของ Pointer
6060
Array ของ Pointer เป%นื้การก&าหนื้ด Array ที่ �มู ส่มูาช�กแตั�ลัะตั�วเป%นื้พอยู่เตัอร)type *ptr_name[size];
หร�อtype *ptr_name[size]
= {value_list};เช�นื้ int *px[10]; px เป%นื้ Array มู ส่มูาช�ก 10 ตั�ว แตั�ลัะตั�วเป%นื้แบบ Pointer ส่&าหร�บเก$บ Address ของข!อมู#ลัแบบ intpx[0] px[1] px[8] px[9]
int *px[10];
px[2] = &rate;
6161
#include <stdio.h> #include <conio.h> #include <string.h>
( ) { * 5int ptr[ ];
[ ] = { 1 0 0 , 2 0 0 , 3 0 0 , 4 0 0 , 5 0 0 } ;
( = 0; < 5; + + ) { [ ] = &[] ; * * 50ptr[i] = ptr[i] + ; printf("y[%d] = %d\n",i ,
y[i]);}
}
y[0] = 150y[1] = 250y[2] = 350y[3] = 450y[4] = 550
6262
#include <stdio.h> #include <conio.h> #include <string.h>
( ) { * 3int pt[ ],i;
x [ ] = {10,20,30}; 0for (i = ; i <= 2 ; ++)
pt[i] = &x[i];for(i = 0; i <=2; ++i) { *pt[i] = x[i] + 10; printf(“x[%d] = %d\
n”,i,*pt[i]); }}
6363
#include <stdio.h>void print_names(char *p[], int n);
void main(){ char *phys[4] = {"Newton", "Einstein", "Fermi", "Heisenberg"}; print_names(phys, 4);}
void print_names(char *p[], int n){ int i; for(i = 0; i < n; i++) printf("%s\n", *p++);}
NewtonEinsteinFermiHeisenberg
64
เนื้��อหา
64
Pointer คื�ออะไร การประกาศตั�วแปร Pointer การส่�งคื�ากลั�บของ Function Pointer ก�บ Array Pointer ก�บ String Array ของ Pointer Pointer ของ Pointer
6565
Pointer ของ Pointer (Indirect Pointer)
เป%นื้ตั�วแปร Pointer ที่ �เก$บ Address ของตั�วแปร Pointer ของข!อมู#ลัอ กตั�วหนื้��งไว!type **ptr_name;
หร�อtype
**ptr1_name, **ptr2_name
เช�นื้ int **pptr_x; เป%นื้ตั�วแปร Pointer ที่ �ช �ไปยู่�ง Pointer คื�อ จำะเก$บ Address ของ Address ที่ �เก$บ Address ของข!อมู#ลัแบบ Integer
6666
#include <stdio.h>#include <conio.h>void main() { int x=20, y =30; //a int value int *px, *py; //pointer to int int **ppx, **ppy; //pointer to pointer to int px = &x; py = &y; ppx = &px; ppy = &py; printf(“x=%d, y=%d\n”, **ppx, **ppy); }
x=20, y =30
6767
#include <stdio.h>main(){ char c='A'; char *p; char **p2; char ***p3;
p = &c; p2 = &p; p3 = &p2;
printf("%c%c%c%c\n", c, *p, **p2, ***p3); printf("%d %p\n", c, &c); printf("%p %p\n", p, &p); printf("%p %p\n", p2, &p2); printf("%p %p\n", p3, &p3);}
%p prints address in hexadecimal.