SlideShare a Scribd company logo
1 of 35
Download to read offline
ฟังก์ชันส่งผ่านอาร์เรย์
Advance Computer Programming
รหัสวิชา 32090207
อ. กิตตินันท์ น้1อยมณี 1
เนื้อหา
1. ทบทวนเรื่อง Pointer
2. Pointer Array
3. Pointer Function
4. Dynamic Array
อ. กิตตินันท์ น้1อยมณี 2
ทบทวนเรื่อง Pointer
อ. กิตตินันท์ น้3อยมณี 3
ทบทวนเรื่อง Pointer
• Pointer คือตัวชี้ ซึ่งถือเป็นลักษณะเด่นของ Operator ในภาษา C
• มีประโยชน์ในการจัดการข้อมูลแบบ Dynamic data structure
• ตัวแปรชนิด Pointer จะเก็บค่า “ที่อยู่” ของหน่วยความจํา (แตกต่างกับ
ตัวแปรชนิดอื่นที่เก็บแค่ค่าจริง)
• รูปแบบการประกาศตัวแปรคือ
ชนิดข้อมูล *ชื่อตัวแปร เช่น int *variable;
ชนิดข้อมูล* ชื่อตัวแปร เช่น int* variable;
อ. กิตตินันท์ น้1อยมณี 4
ทบทวนเรื่อง Pointer
อ. กิตตินันท์ น้1อยมณี 5
Pointer Array
อ. กิตตินันท์ น้6อยมณี 6
Array เก็บ Pointer 3 ตัว
อ. กิตตินันท์ น้1อยมณี 7
Pointer 1 ตัวที่ชี้ Array ไม่จํากัดแถว แถวละ 3 Column
0 1 2
a[0] Data Data Data
a[1] Data Data Data
…
…
…
…
a[0] Data Data Data
อ. กิตตินันท์ น้1อยมณี 8
การประกาศใช้
int (*a)[3]; หรือ a = new int[n][3];
ค่าที่ได้คือ
Pointer 1 ตัวที่ชี้ Array ไม่จํากัดแถว แถวละ 3 Column
อ. กิตตินันท์ น้1อยมณี 9
Pointer Function
อ. กิตตินันท์ น้10อยมณี 10
Reference
อ. กิตตินันท์ น้1อยมณี 11
ตัวแปร Address Value
B 1000 10
1001
*A 1002 1000
1003
**P 1004 1002
1005
1006
Reference
อ. กิตตินันท์ น้1อยมณี 12
ดังนั้น *a === **p
Pointer Function
อ. กิตตินันท์ น้1อยมณี 13
Dynamic Array
อ. กิตตินันท์ น้14อยมณี 14
ที่มา
int a[5] ;
อ. กิตตินันท์ น้1อยมณี 15
int *b ;
0 1 2 3 4
a
หมายเหตุ : b ก็เป็น Array ได้เหมือนกัน เรียกว่า Dynamic Array
ที่มา
• เพราะไม่รู้ว่าต้องการใช้เท่าไหร่
• จะประกาศล่วงหน้าเยอะๆ ก็ไม่ดี
• ดังนั้น Pointer จึงเป็นทางออก
• เอาไปทําเป็น Array ที่มีสมาชิกกี่ตัวก็ได้ โดยการเอาไปจองหน่วยความจํา
• วิธีการมีดังนี้
อ. กิตตินันท์ น้1อยมณี 16
Dynamic Array
int *b;
b = new int[n];
อ. กิตตินันท์ น้1อยมณี 17
0 1 2 … n
b
เอา b ไปจอง Address
Dynamic Array
• ดังนั้น หากต้องการจอง 2 มิติก็ต้องทําลักษณะนี้ ?
int **c ;
c = new int [n][m] ;
อ. กิตตินันท์ น้1อยมณี 18
Dynamic Array
• การจอง 2 มิติ ที่ถูกต้องควรทําในลักษณะนี้
int (*c)[4];
• ข้อสังเกตคือ ตรงไหนที่มี Pointer หมายความว่ายืดหยุ่นได้
int **c;
c = new int [n][4];
อ. กิตตินันท์ น้1อยมณี 19
ลักษณะการทํางาน
สมมติเรามี Array 2 มิติ int a[3][4];
อ. กิตตินันท์ น้1อยมณี 20
0 1 2 3
0 0 1 2 3
1 4 5 6 7
2 8 9 10 11
j ตัวบอก Column
i ตัวบอก Row
ลักษณะการทํางาน
สร้าง 1 มิติ เป็น 2 มิติ โดยการตัด Array แล้วเอามาต่อกัน
อ. กิตตินันท์ น้1อยมณี 21
0 1 2 3
4 5 6 7
8 9 10 11
ลักษณะการทํางาน
นั่นคือเอา Array ขนาด 12 มาแทน Array 3x4
อ. กิตตินันท์ น้1อยมณี 22
0 1 2 3 4 5 6 7 8 9 10 11
การสร้างคือ
int *a;
a = new int [ 3 x 4 ];
ลักษณะการทํางาน
แบบ 2D
a[1][2]
a[ i ][ j ]
แบบ 1D
a[6] = a[ 1 x 4 + 2 ]
a[ i x col + j]
อ. กิตตินันท์ น้1อยมณี 23
0 1 2 3
0
1
2
0 1 2 3 4 5 6 7 8 9 10 11
ลองเขียนโปรแกรม
ตัวอย่างที่ 1 Dynamic Array (การจองและการใช้งาน)
อ. กิตตินันท์ น้1อยมณี 24
ลองเขียนโปรแกรม
ตัวอย่างที่ 1 Dynamic Array (การจองและการใช้งาน)
อ. กิตตินันท์ น้1อยมณี 25
Error ที่บรรทัดนี้เพราะประกาศแบบนี้ไม่ได้
รับค่าแบบนี้ไม่ได้ ต้องแก้ด้วย
ลองเขียนโปรแกรม
ตัวอย่างที่ 1 Dynamic Array (การจองและการใช้งาน)
อ. กิตตินันท์ น้1อยมณี 26
ประกาศ a เป็น Pointer ซะเลย
ลองเขียนโปรแกรม
• ในตัวอย่างที่ 1 นั้น Code ยังคงดูวุ่นวายอยู่
• ดังนั้นเราต้องแยกงานเฉพาะอย่าง ให้เป็น Function จะ
ดีกว่า เช่น ส่วนของการเก็บข้อมูล
อ. กิตตินันท์ น้1อยมณี 27
ลองเขียนโปรแกรม
ตัวอย่างที่ 2 ลด Code โดยการแยกเป็น Function
อ. กิตตินันท์ น้1อยมณี 28
Get Array 2D
Get Array 2D
a [ ]
col
row
col
row
a [ ]
ออกทาง Argument ออกทาง Return
ออกทาง Argument ออกทาง Return
แผน 1
แผน 2
ลองเขียนโปรแกรม
ข้อสังเกต:
• Array ขาออก เป็นเหมือน Pointer 1 ระดับอยู่แล้ว
• ดังนั้นการจะเอาข้อมูลออกจะต้องมีมากกว่าเดิมอยู่ 1 ระดับ
Get Array 2D
a [ ]
col
row
ลองเขียนโปรแกรม
ตัวอย่างที่ 2 ลด Code โดยการแยกเป็น Function
อ. กิตตินันท์ น้1อยมณี 30
แผน 1
เพราะต้องการเข้าใช้ตัว
แปร arr ที่อยู่ใน main
จึงต้องใช้ *a
ลองเขียนโปรแกรม
ตัวอย่างที่ 2 ลด Code โดยการแยกเป็น Function
อ. กิตตินันท์ น้1อยมณี 31
แผน 1
MainMain
ต้อง Prototype ไว้ก่อน
หมายเหตุ: ประโยชน์ของ Function คือสามารถเรียกใช้คําสั่งเดิมๆ โดยเปลี่ยนแค่ตัวแปร
เท่านั้น
ลองเขียนโปรแกรม
ตัวอย่างที่ 2 ลด Code โดยการแยกเป็น Function
อ. กิตตินันท์ น้1อยมณี 32
แผน 1
Function Main
a === &arr
*a === arr
row === &r
*row === r
ใน Function เก็บ
Address ของ arr
ลองเขียนโปรแกรม
ตัวอย่างที่ 2 ลด Code โดยการแยกเป็น Function
อ. กิตตินันท์ น้1อยมณี 33
แผน 2
ประกาศขึ้นมาชั่วคราว
ลองเขียนโปรแกรม
ตัวอย่างที่ 2 ลด Code โดยการแยกเป็น Function
อ. กิตตินันท์ น้1อยมณี 34
แผน 2
ดังนั้นจึงใช้มันได้เลย
(ไม่เหมือนแผน 1)
Return a ได้เลย
ลองเขียนโปรแกรม
ตัวอย่างที่ 2 ลด Code โดยการแยกเป็น Function
อ. กิตตินันท์ น้1อยมณี 35
แผน 2
MainMain
ต้องลบหน่วยความจําด้วย

More Related Content

Featured

How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
ThinkNow
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
Kurio // The Social Media Age(ncy)
 

Featured (20)

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 

(Big One) C Language - 01 ฟังก์ชันส่งผ่านอาร์เรย์