C Programming By P*Oak

Download Report

Transcript C Programming By P*Oak

*
ความหมายของพอยเตอร์
การใช้งานพอยเตอร์
พอยเตอร์กับอาร์เรย์ และพอยเตอร์กับสตรัคเจอร์
int number = 2;
number
1. ชื่ออ้างอิง
number
2
2
AA02 AA04 AA05
AA02
2. address
*
3.ค่า
1. อ้างอิงค่าที่เก็บใน address
รูปแบบ ชื่อตัวแปร
เช่น number จะหมายถึงค่า 2
number
2
2. อ้างอิงค่า address
รูปแบบ &ชื่อตัวแปร
AA02
เช่น &number จะหมายถึง AA02
*
*
1. ตัวแปรพืน้ ฐาน (เก็บค่าตามชนิดข้อมูล)
รูปแบบ
เช่น
ชนิดข้อมูล ชื่อตัวแปร;
float a = 3.14;
2. ตัวแปรพอยเตอร์ (เก็บค่า address)
รูปแบบ ชนิดข้อมูล *ชื่อตัวแปร;
เช่น float *pA = &a;
หมายเหตุ ชนิดข้อมูลของตัวแปรพอยเตอร์ต้องตรงกับ
ชนิดข้อมูลที่จะทาการเก็บ address
a
3.14
AA00
pA
AA00
xxxx
*ตัวแปรพอยเตอร์เป็นตัวแปรเก็บค่าตาแหน่ง
แต่การเก็บค่า address ให้เสมือนว่าทาการชี้ไปที่กล่อง
adress นั้น
a
float a = 3.14;
3.14
AA00
pA
float *pA = &a;
xxxx
*
*ชนิดข้อมูลของพอยเตอร์ ไม่ตรงกับชนิดข้อมูลที่จะทาการชี้
char x = ‘a’;
int *pX = &x; ผิด compile error
char x = ‘a’;
ชนิดข้อมูลต้อง Match กัน
char *pX = &x;
*
หลังจากทาการชี้ด้วยคาสั่งต่อไปนี้แล้ว…
char x = ‘a’;
char pX = &x;
x
‘a’
AA22
การอ้างอิง ข้อมูล ใน address
รูปแบบ *ชื่อตัวแปรพอยเตอร์
เช่น *pX จะหมายถึง ‘a’(เหมือนอ้างอิง x)
การอ้างอิง ค่า address
รูปแบบ ชื่อตัวแปรพอยเตอร์
เช่น pX จะหมายถึง AA22
*
pX
xxxx
#include <stdio.h>
pA
main()
{
int a = 2;
int *pA = &a;
xxxx
printf(“a = %d”, a);
printf(“*pA = %d”, *pA);
printf(“pA(hex) = %p”, pA);
printf(“pA(dec) = %d”, pA);
}
a
2
AA10
%p คือรหัสแสดง address
เป็นเลขฐาน 16
a = 2
*pA = 2
pA(hex) = AA10
pA(dec) = 43536
%d หรือ %u คือรหัสแสดง address
เป็นเลขฐาน 10
*
นาพอยเตอร์มากาหนดค่าก่อนทาการชี้
int *pt;
*pt = 2; ผิด compile error
pt
pointer ยังไม่ได้ชี้ไปที่ address ใด
????
xxxx
*
int a[3] = {1,2,3};
int *pA;
การให้พอยเตอร์ชี้ไปที่ indexที่ 0 (ทาได้ 2 กรณี)
pA = a; // อ้างอิงชื่อโดยตรง
หรือ
pA = &a[0]; // อ้างอิงโดยใช้ &
การให้พอยเตอร์ชี้ไปที่ indexที่อื่นๆ
pA = &a[index];
*
int a[3] = {1,2,3};
int *pA = a;
pA
B20
1
xxxx
B22
2
B24
3
a[0] a[1] a[2]
pA[0] pA[1] pA[2]
*pA *(pA+1) *(pA+2)
*
ชื่ออ้างอิงค่าใน
array
(3 แบบ)
int a[4] = {1,2,3,4};
int *pA = &a[1];
pA
xxxx
B20
B22
B24
B26
1
2
3
4
a[0] a[1] a[2] a[3]
pA[-1] pA[0] pA[1] pA[2]
index ของ
พอยเตอร์ตัวที่ชี้ไป
จะเริ่มจาก 0
*(pA-1) *pA
*
*(pA+1) *(pA+2)
ทบทวนการอ้างอิงค่าในพอยเตอร์
int *pt;
- pt จะหมายถึงค่า address
- *pt จะหมายถึง ข้อมูลใน address นั้น
*pt+n ความหมายคือ เอาค่าข้อมูลใน address มาบวก n
*(pt+n) ความหมายคือ อ้างค่าข้อมูลใน address + n
ใช้อ้างอิงในอาร์เรย์จากหัวข้อที่แล้ว
*
*
ความหมายของชนิดข้อมูลแบบโครงสร้าง
การสร้างสตรัคเจอร์
พอยเตอร์กับสตรัคเจอร์
ข้อจากัดอาร์เรย์คือ ชนิดข้อมูลต้องเหมือนกันทั้งชุด
สตรัคเจอร์ คือ ชนิดข้อมูลที่รวบรวมชนิดข้อมูลพื้นฐานมาสร้าง
เป็นกลุ่มข้อมูลใหม่ (โดยชนิดข้อมูลไม่ต้องเหมือนกันก็ได้)
struct Student
{
char firstName[10];
char lastName[10];
int id;
float grade;
};
*
struct ชื่อโครงสร้างหลัก
{
สมาชิก 1;
สมาชิก 2;
สมาชิก 3;
struct ชื่อโครงสร้างย่อย
{
สมาชิก 1;
สมาชิก 2;
สมาชิก 3;
};
};
struct ชื่อโครงสร้าง
{
สมาชิก 1;
สมาชิก 2;
สมาชิก 3;
….
….
};
structure
structure ซ้อน structure
*
ต้องสร้างตัวแปรของ structure ขึ้นมา (ทาได้ 2 กรณี)
ประกาศแบบที่ 1
ประกาศแบบที่ 2
struct ชื่อโครงสร้าง
{
สมาชิก 1;
สมาชิก 2;
สมาชิก 3;
….
….
}ชื่อตัวแปร;
ชื่อตัวแปร สามารถเป็น
อาร์เรย์ หรือพอยเตอร์ก็ได้
struct ชื่อโครงสร้าง
{
สมาชิก 1;
สมาชิก 2;
สมาชิก 3;
….
….
};
struct ชื่อโครงสร้าง ชื่อตัวแปร;
*
std1
firstName
lastName
struct Student
{
char firstName[10];
char lastName[10];
int id;
float grade;
}std1;
id
grade
*
struct Student
{
char Name[10];
int id;
float grade;
}std1 = {“oak”, 1359, 4.00};
หรือ
struct Student
{
char Name[10];
int id;
float grade;
};
struct Student std1
= {“oak”, 1359, 4.00};
*
std1
firstName
“oak”
id
1359
grade
4.00
ต้องใส่ชนิดข้อมูลให้ตรงตามลาดับที่ประกาศไว้
std1 = {1359,”oak”, 4.00}; ผิด
การอ้างอิงสมาชิกทาได้โดยอ้างอิงผ่าน . (dot)
รูปแบบ
ชื่อตัวแปรโครงสร้าง.สมาชิก
หรือกรณีเป็น structure ซ้อน structure
รูปแบบ
ตัวแปรโครงสร้างหลัก.ตัวแปรโครงสร้างใน.สมาชิกใน
struct Student
{
char Name[10];
int id;
float grade;
};
struct Student std1;
*
std1.Name
std1.id
std1.grade
struct Student
{
char Name[10];
int id;
float grade;
}std1;
strcpy(std1.Name, “oak”);
std1.id = 1359;
std1.grade = 4.00;
std1
firstName
“oak”
id
1359
grade
4.00
การกาหนดค่าให้สมาชิกโครงสร้างแบบ ข้อความ จะกาหนดผ่านฟังก์ชั่น strcpy()
รูปแบบ strcpy(ตัวแปร, ข้อความ)
หมายเหตุ ต้องทาการ include string.h เข้ามาในโปรแกรมด้วย
*
std1
struct Student
{
char Name[10];
int id;
float grade;
}
struct Student std1;
firstName
id
grade
struct Student *pStd1;
pStd1 = &std1;
pStd1
ตัวแปรพอยเตอร์ชี้ไปที่ address
ของตัวแปรโครงสร้าง
*
หลังจากทาการชี้ไปที่ตัวแปรโครงสร้างแล้ว
struct Student
{
char Name[10];
int id;
float grade;
}
struct Student std1;
struct Student *pStd1;
pStd1 = &std1;
*
สิ่งที่สามารถอ้างอิงหลังทาการชี้
pStd1->Name เหมือน std1.Name
pStd1->id เหมือน std1.id
pStd1->grade เหมือน std1.grade
รูปแบบ ชื่อตัวแปรพอยเตอร์->สมาชิก