Computer Programming การเขียนโปรแกรมคอมพิวเตอร์

Slides:



Advertisements
งานนำเสนอที่คล้ายกัน
Array ธนวัฒน์ แซ่ เอียบ. The concept of array อาเรย์ : กลุ่มของข้อมูลที่มีชนิดเดียวกันและถูก จัดเก็บเรียงลำดับต่อเนื่องกัน ตัวแปร x สามารถจัดเก็บค่ามากกว่า.
Advertisements

การเขียนคำสั่งควบคุม การทำงานขั้นพื้นฐาน
หลักการโปรแกรม 1 Lecture 14: โปรแกรมย่อย ( การส่งพารามิเตอร์ แบบ pass by reference)
LAB ตัวแปร : Variables ในการเขียนโปรแกรมเราต้องการให้โปรแกรม จดจำค่าต่างๆ ไว้ เช่นเมื่อรับค่าข้อมูลจาก ผู้ใช้ หรือค่าที่ได้จากการคำนวณใดๆ ค่า.
Chapter 10 Arrays Dept of Computer Engineering Khon Kaen University.
ชนิดของข้อมูล และการคำนวณทางคณิตศาสตร์
1. Select query ใช้สำหรับดึงข้อมูลที่ต้องการ
กาญจนา ทองบุญนาค สาขาวิชาคอมพิวเตอร์ คณะวิทยาศาสตร์และเทคโนโลยี
หน่วยที่ 6 แถวลำดับ (Array)
ตัวแปร และชนิด ข้อมูล. ตัวแปร การกำหนดตัวแปรเป็นการใช้ ชื่อตัวแปรแทน ตำแหน่งบนหน่วยความจำ สำหรับเก็บ ข้อมูลระหว่างการ ประมวลผล ซึ่งอาจเป็นข้อมูลนำเข้า.
MEE 231 Computer Programming สัปดาห์ที่ 6 ภาษา C หลักการของโปรแกรม ชนิดของตัวแปร การดำเนินการ คำสั่งการรับค่าตัวแปร และการแสดงผล.
ARRAY & PRINTF สาขางานคอมพิวเตอร์ธุรกิจอาจารย์จิรา ภรณ์ เขตกุฎี
LOGO ภาษาซี 1.1 อ. กฤติเดช จินดาภัทร์. LOGO ตัวอย่างโค้ดภาษาซี
เกม คณิตคิดเร็ว.
Computer Programming การเขียนโปรแกรมคอมพิวเตอร์
โครงสร้างโปรแกรมภาษา C
การจัดการโปรเซส T.Kunlaya Charoenmongkonvilai
stack #2 ผู้สอน อาจารย์ ยืนยง กันทะเนตร
รายการ(List) [1] ผู้สอน อาจารย์ ยืนยง กันทะเนตร
อาจารย์อภิพงศ์ ปิงยศ บทที่ 8 : TCP/IP และอินเทอร์เน็ต Part2 สธ313 การสื่อสารข้อมูลและเครือข่ายคอมพิวเตอร์ทางธุรกิจ อาจารย์อภิพงศ์
อาจารย์อภิพงศ์ ปิงยศ บทที่ 8 : TCP/IP และอินเทอร์เน็ต Part3 สธ313 การสื่อสารข้อมูลและเครือข่ายคอมพิวเตอร์ทางธุรกิจ อาจารย์อภิพงศ์
บทที่ 5 อินพุตและเอาต์พุต
โปรแกรมย่อย อาจารย์สมเกียรติ ช่อเหมือน
ครูวีรธรรม เทศประสิทธิ์ แผนกวิชาคอมพิวเตอร์ธุรกิจ
Computer Programming การเขียนโปรแกรมคอมพิวเตอร์
Chapter 9 ตัวชี้ pointer.
Principles of Problem Solving and Basic Programming หลักการแก้ปัญหาและการเขียนโปรแกรมเบื้องต้น2(1-2-3) สัปดาห์ที่ 3 ชนิดข้อมูลและการแทนชนิดข้อมูลการประกาศตัวแปร.
การประยุกต์ Logic Gates ภาค 2
Principles of Problem Solving and Basic Programming หลักการแก้ปัญหาและการเขียนโปรแกรมเบื้องต้น2(1-2-3) สัปดาห์ที่ 4 เครื่องหมาย ตรรกะศาสตร์ และการดำเนินการทางตรรกะศาสตร์
สถาปัตยกรรมคอมพิวเตอร์ (Computer Architecture)
อาจารย์อภิพงศ์ ปิงยศ บทที่ 7 : TCP/IP และอินเทอร์เน็ต Part2 สธ313 การสื่อสารข้อมูลและเครือข่ายคอมพิวเตอร์ทางธุรกิจ อาจารย์อภิพงศ์
Computer Programming การเขียนโปรแกรมคอมพิวเตอร์
(Array, Pointer, Structure)
บทที่ 10 อาร์เรย์ (Array)
แล้วทำการเรียงลำดับข้อมูลใหม่โดยเรียงจากน้อยไปหามาก
รายการ(List) [3] ผู้สอน อาจารย์ ยืนยง กันทะเนตร
STACK สแตก(stack) เป็นโครงสร้างข้อมูลแบบเชิงเส้น ที่มีการใส่ข้อมูลเข้า และนำข้อมูลออกเพียงด้านเดียว ดังนั้น ข้อมูลที่เข้าไปอยู่ใน stack ก่อนจะออกจาก stack.
ตัวแปรและชนิดของข้อมูล
BC320 Introduction to Computer Programming
โครงสร้างภาษา C Arduino
บทที่ 7 การเขียนโปรแกรม แบบวนรอบทำซ้ำ (Loop)
stack #1 ผู้สอน อาจารย์ ยืนยง กันทะเนตร
บทที่ 13 การจัดการไฟล์ (File Management)
ตัวแปรกับชนิดของข้อมูล
คำสั่ง Create , Insert, Delete, Update
ขั้นตอนการออกแบบ ผังงาน (Flow Chart)
Computer Programming การเขียนโปรแกรมคอมพิวเตอร์
กลุ่มเกษตรกร.
Data storage II Introduction to Computer Science ( )
บริษัท พัฒนาวิชาการ (2535) จำกัด
บทที่ 9 การทำซ้ำ (Loop).
การเขียนภาษาจาวาเบื้องต้น
Week 5 C Programming.
ตัวแปรชนิดพอยเตอร์ (Pointer)
Variable Constant.
การสร้างแบบสอบถาม และ การกำหนดเงื่อนไข.
Data storage II Introduction to Computer Science ( )
หน่วยที่ 6 อะเรย์ของอักขระ
บทที่ 7 การประมวลผลอาร์เรย์
OPERATOR ภาษาปาสคาล (Pascal)
ค่ารูรับแสง - F/Stop ค่ารูรับแสงที่มีค่าตัวเลขต่ำใกล้เคียง 1 มากเท่าไหร่ ค่าของรูรับแสงนั้นก็ยิ่งมีความกว้างมาก เพราะเราเปรียบเทียบค่าความสว่างที่ 1:1.
2 โครงสร้างข้อมูลแบบสแตก (STACK).
บทที่ 2 โครงสร้างข้อมูลแบบแถวลำดับหรืออาร์เรย์ (Array)
Chapter 3 : Array.
Array Sanchai Yeewiyom
Array: One Dimension Programming I 9.
ฟังก์ชันของโปรแกรม Computer Game Programming
Decision: Single and Double Selection (if and if-else statement)
พอยเตอร์ #include <stdio.h> void main() { int age; int *pointer;
Computer Game Programming
ใบสำเนางานนำเสนอ:

Computer Programming การเขียนโปรแกรมคอมพิวเตอร์ สัปดาห์ที่ 8 ตัวแปรชี้ตำแหน่ง(Pointer)

objectives เพื่อให้นิสิตรู้จักและเข้าใจตัวแปรชี้ตำแหน่งในภาษาซี สามารถเขียนโปรแกรมภาษาซีโดยใช้ตัวแปรชี้ตำแหน่งได้ สามารถนำความรู้เรื่องตัวแปรชี้ตำแหน่งไปประยุกต์เพื่อแก้ไขโจทย์ปัญหาในชีวิตประจำวันได้ได้อย่างถูกต้องเหมาะสม

Arithmetic & Pointer Operator Outline 1 Pointer 2 p Arithmetic & Pointer Operator 3 Pointer Array 4 Pointer to Pointer 5 Assignment #9

C Programming Structure พรีโปรเซสเซอร์ไดเร็คทีฟ #include<file.h> type function_name(type); type variable; int main() { statement-1; ... statement-n; return 0; } type function_name(type variable) return(var); ฟังก์ชันโพรโทรไทพ์ ส่วนหัวโปรแกรม ตัวแปรชนิดโกบอล ตัวแปรชนิดโลคอล ฟังก์ชันหลัก คำสั่ง ส่วนตัวโปรแกรม ฟังก์ชันย่อย

เมื่อมีการประกาศตัวแปรใดๆ เช่น int number; Variable Declaration เมื่อมีการประกาศตัวแปรใดๆ เช่น int number; char letter; คอมไพเลอร์จะทำการจองพื้นที่ในหน่วยความจำสำหรับเก็บค่าตัวแปร number และค่าตัวแปร letter สมมุติว่าเป็นตำแหน่งหน่วยความจำที่ 10000 และ 10002 เมื่อมีการกำหนดจะทำการจองหน่วยความจำดังนี้ number = 5; letter = ‘A’; ที่อยู่ ข้อมูล 10000 10001 10002 10003 10004 10005 10006 10007 5 ‘A’ number letter

Variable Declaration (cont.) ที่อยู่ของข้อมูล (Address) ข้อมูล ประเภท ชื่อตัวแปร ขนาด 0000 15 int count 2 bytes 0001 0002 3.1415 0003 float pi 4 bytes 0004 0005 0006 'A' char ch 1 bytes … … XXXX … …

Size of Variable ชนิดของตัวแปร ขนาด ค่าต่ำสุด ค่าสูงสุด unsigned char 8 bit 255 char -128 127 unsigned int 16 bit 32 bit 65,535 4,294,967,295 short int -32,768 32,767 int -2,147,483,648 2,147,483,647 unsigned long long float 3.410-38 3.4 1038 double 64 bit 1.7 10-308 1.710308 long double 80 bit 3.4 10-4932 3.4104932

Size of Variable (cont.) 10 80 long double 8 64 double 4 32 float 1 unsigned char char unsigned long long 2 16 unsigned int int Bytes Bits Type

Basic Knowledge of Pointer พอยน์เตอร์ หมายถึง ตัวแปรใดๆ ที่ใช้เก็บค่าของตำแหน่งที่อยู่ (Address) ของตัวแปรหรือข้อมูล เมื่อเข้าถึงที่อยู่โดยพอยน์เตอร์ได้ก็จะสามารถกระทำกับข้อมูลได้ไม่ว่าจะเป็นการนำออกมาแสดงผลหรือกระทำกับตัวแปรอื่นๆ ตัวแปรชนิดพอยน์เตอร์มีความเร็วในการทำงานสูง ตัวแปรชนิดพอยน์เตอร์จะเก็บค่าที่อยู่ของหน่วยความจำหลัก ซึ่งต่างกับตัวแปรปกติที่เก็บค่าที่แท้จริงของข้อมูล นั่นคือการใช้ตัวแปรชนิดพอยน์เตอร์จะเป็นการเข้าถึงข้อมูลหรือเป็นการอ้างถึงตำแหน่งที่เก็บข้อมูล

* คือ เครื่องหมายแสดงว่าเป็นตัวแปรประเภทตัวชี้ Pointer Declaration type *pointer_name type คือ ชนิดของตัวแปรประเภทตัวชี้ (pointer) * คือ เครื่องหมายแสดงว่าเป็นตัวแปรประเภทตัวชี้ pointer_name คือ ชื่อของตัวแปรประเภทตัวชี้ ตัวอย่างการใช้ตัวชี้ int * ptr_int; /* pointer to integer */ float * ptr_float;// pointer to float char * ptr_char; /* pointer to char */

Pointer Declaration (cont.) ตัวแปรพอยน์เตอร์ทำหน้าที่เก็บตำแหน่งที่อยู่ของตัวแปรที่ชี้ นั่นคือทำหน้าที่ชี้ไปยังตัวแปรตัวอื่น ดังนั้นการประกาศตัวแปรพอยน์เตอร์ ต้องสอดคล้องกับชนิดของตัวแปรตัวอื่น ที่ต้องการให้ตัวแปรพอยน์เตอร์นั้นจะชี้ไป เช่น char *prt; int *ip , *temp; double *dp; ประกาศตัวแปร prt ให้เป็นตัวแปรพอยน์เตอร์ที่ชี้ไปยัง ตัวแปรชนิด char ประกาศตัวแปร ip และ ตัวแปร temp เป็นตัวแปรพอยน์- เตอร์ที่ชี้ไปยังตัวแปรชนิด int ประกาศตัวแปร dp เป็นตัวแปรพอยน์เตอร์ที่ชี้ไป ยังตัวแปรชนิด double

Referencing Operator “&” ตัวดำเนินการ & ส่งกลับเลขที่อยู่ (Address) ของวัตถุ XXXX int count; 15 int *ptr; XXXX ptr = &count; /* ptr มีค่าเป็น XXXX */

How the pointer work? ตัวดำเนินการอ้างถึงที่อยู่ (Reference/Address operator) จะใช้เครื่องหมาย & นำหน้าตัวแปร ซึ่งหมายถึงตำแหน่งที่อยู่ของตัวแปรนั้นในหน่วยความจำ #include <stdio.h> void main(void){ int z = 10; int *ZPtr; ZPtr = &z;/*ตัวแปรชนิดตัวชี้เก็บเลขที่อยู่ของตัวแปร z*/ }

Referencing Operator “&” (cont.) ที่อยู่ของข้อมูล (Address) ข้อมูล ประเภท ชื่อตัวแปร ขนาด 0000 15 int count 2 bytes 0001 0002 3.1415 0003 float pi 4 bytes 0004 0005 0006 'A' char ch 1 bytes … ptr = &count ตัวชี้ ptr ชี้ไปที่อยู่ของ ตัวแปรชื่อ count XXXX 0000 …

Referencing Operator “&” (cont.) ที่อยู่ของข้อมูล (Address) ข้อมูล ประเภท ชื่อตัวแปร ขนาด 15 int count 2 bytes 0000 3.1415 float pi 4 bytes 0002 'A' 0006 char ch 1 bytes 0006 char *ptr_char = &ch; 0002 float *ptr_float = π 0000 int *ptr_int = &count;

Dereference Operator “*” เป็น ตัวดำเนินการเชิงอ้อม (Indirection) หรือ กลับการอ้างอิง (Dereferencing) ตำแหน่งที่ XXXX 15 int 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] */

Dereference Operator “*” (cont.) ตัวดำเนินการอ้างถึง (Dereferencing operator) ใช้เครื่องหมาย * นำหน้าตัวแปรพอยน์เตอร์ จะเป็นการอ้างถึงค่าข้อมูลที่ตัวแปรพอยน์เตอร์นั้นชี้ไป ซึ่งค่าข้อมูลนี้สามารถเปลี่ยนแปลงค่าได้ #include <stdio.h> void main(void){ int x; int y = 20; int *z; z = &y; x = *z;/*เป็นการอ้างถึงค่าข้อมูลที่ตัวแปรชนิดตัวชี้*/ printf (“%d”, x)/*ได้ผลลัพธ์คือ 20 */ } ให้ตัวแปรพอยน์เตอร์ z ชี้ไปยังแอดเดรสของตัวแปร y โดยการให้ z เก็บตำแหน่งแอดเดรสของ y นั้นเอง

How to show the pointer value? ฟังก์ชัน printf สามารถแสดงตำแหน่งที่อยู่ (address) ได้โดยใช้ เครื่องหมาย %p เพื่อแสดงตำแหน่งเป็นเลขฐานสิบหก เครื่องหมาย %u เพื่อแสดงตำแหน่งเป็นเลขฐานสิบ ผลลัพธ์ที่ได้จะอยู่ในรูปแบบ XXXX:YYYY หรือ XXXX ขึ้นอยู่กับ Memory Model ที่ใช้

Example 1 D 0000 letter 19 0002 num 26.09 0004 point ???? pt_letter #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 = &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; } D 0000 letter 19 0002 num 26.09 0004 point ???? pt_letter ???? pt_num ???? pt_point 0000 0002 0004

Example 1 (cont.) D 0000 letter 19 0002 num 26.09 0004 point 0000 ???? Address of letter = 0000 Address of num = 0002 Address of point = 0004 0000 ???? pt_letter 0002 ???? pt_num 0004 ???? pt_point

Example 2 num1 price1 hint1 113 4.85 J --x-- --y-- --z-- pt_num 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; } num1 price1 hint1 113 4.85 J --x-- --y-- --z-- pt_num pt_price pt_hint --x-- --y-- --z-- num2 hint2 113 J

Example 2 (cont.) num1 price1 hint1 113 4.85 J --x-- --y-- --z-- Variable num1 = 113 Variable price1 = 4.850000 Variable hint2 = J --x-- --y-- --z-- pt_num pt_price pt_hint --x-- --y-- --z-- num2 hint2 113 J

The Other Format of Reference and Dereference Operators FFF4 7 a ???? FFF4 aPtr printf ("The value of a is %d\n" "The value of *aPtr is %d\n\n", a, *aPtr); The address of a is FFF4 The value of aPtr is FFF4 The value of a is 7 The value of *aPtr is 7 Proving that * and & are complements of each other. &*aPtr = FFF4 *&aPtr = FFF4 printf ("The address of a is %p\n" "The value of aPtr is %p\n\n", &a, aPtr); printf ("Proving that * and & are complements of " "each other.\n&*aPtr = %p\n*&aPtr = %p\n", &*aPtr, *&aPtr); 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 */ The value of a is 7 The value of *aPtr is 7 The address of a is FFF4 The value of aPtr is FFF4 Proving that * and & are complements of each other. &*aPtr = FFF4 *&aPtr = FFF4

Summary of Pointer Process 1906 variable1 d การประกาศตัวแปรพอยน์เตอร์ type *pt_name; pt_name แสดงตำแหน่งข้อมูลด้วย “&” 1906 ---- ---- variable2 แสดงข้อมูลด้วย “*” d variable2 = *pt_name;

Arithmetic & Pointer Operator Outline 1 Pointer 2 p Arithmetic & Pointer Operator 3 Pointer Array 4 Pointer to Pointer 5 Assignment #9

Arithmetic & Pointer Operator คณิตศาสตร์ของตัวแปรชี้ตำแหน่งสามารถทำได้เพียง การบวก การลบ การเพิ่มค่าและการลดค่าเท่านั้น ไม่สามารถกระทำการคูณ หาร หรือการเลื่อนบิท เครื่องหมายทางคณิตศาสตร์ของพอยน์เตอร์ได้แก่ + หมายถึง การบวก – หมายถึง การลบ ++ หมายถึง การเพิ่มค่าครั้งละ 1 –- หมายถึง การลดค่าครั้งละ 1

Arithmetic & Pointer Operator (cont.) เครื่องหมาย “+” และ “–” ใช้ได้เฉพาะตัวแปรชี้ตำแหน่งชนิดเลขจำนวนเต็มเท่านั้น การบวกและลบตัวแปรชี้ตำแหน่งคือ การเลื่อนไปชี้ในตำแหน่งที่อยู่ของตัวแปรตามที่กำหนด กรณีที่ตัวแปร ip ชี้ที่ตำแหน่ง 1000 เมื่อทำคำสั่งต่อไปนี้ int *ip; ip = &x; ip = ip + 9; ค่าตัวแปรพอยน์เตอร์ “ip” จะเลื่อนไปชี้ที่ตำแหน่ง “1018”

Arithmetic & Pointer Operator (cont.) การเพิ่มค่าครั้งละ 1 ของตัวแปรตัวชี้จะเป็นการเลื่อนตัวชี้พอยน์เตอร์ตามชนิดของตัวแปรที่ทำการชี้ เช่น พอยน์เตอร์ “ip” ชี้ตัวแปรประเภทจำนวนเต็มที่ตำแหน่ง “1000” เมื่อกำหนด ip++; คอมไพเลอร์จะทำการเลื่อนตัวชี้ “ip” ไปยังตำแหน่ง “1002” เพราะจำนวนเต็มเก็บ 2 ไบท์ int *ip; char *cp; float *fp; fp fp+1 ip ip+1 ip+2 1000 1001 1002 1003 1004 1005 1006 1007 ตำแหน่งที่อยู่ cp cp+1 cp+2 cp+3 cp+4 cp+5 cp+6 cp+7

Arithmetic & Pointer Operator (cont.) นอกจากการกระทำทางคณิตศาสตร์ ตัวชี้ยังมีความสามารถทางตรรกะ(เปรียบเทียบกันได้) เช่นเดียวกันกับตัวแปรปกติ 1. == หมายถึง ตำแหน่งเดียวกัน 2. != หมายถึง ต่างตำแหน่งกัน 3. > หมายถึง ตำแหน่งสูงกว่า 4. >= หมายถึง ตำแหน่งสูงกว่าหรือเท่ากัน 5. < หมายถึง ตำแหน่งต่ำกว่า 6. <= หมายถึง ตำแหน่งต่ำกว่าหรือเท่ากัน เช่น &x[3] จะน้อยกว่า &x[5] เป็นต้น

Pointer & Array ip = &x[0]; // ip = x; ตัวชี้และอาร์เรย์มีความสัมพันธ์กันอย่างมาก บ่อยครั้งมีการจัดการกับสมาชิกของอะเรย์โดยใช้ตัวแปรพอยน์เตอร์ เช่น กำหนดให้ x[10] แล้วทำลำดับต่อไปนี้ ip = &x[0]; // ip = x; y = *ip; // เป็นการนำค่าที่ x[0] ส่งให้ตัวแปร y ที่ตำแหน่งใดๆ สามารถอ้างได้โดยใช้พอยน์เตอร์บวกตัวชี้ตำแหน่ง ++ip; // (++ip) y = *ip; // เป็นการนำค่าที่ x[1] ส่งให้ตัวแปร y

Array Pointer การนำตัวชี้อ้าง ตำแหน่งอาเรย์ต่างๆ (Array Pointer) พอยน์เตอร์ ตำแหน่งอาเรย์ ip X[0] X[1] ip+1 X[]={1,2,3,4,5,6} *(ip+4) = ? X[2] ip+2 ip+3 X[3] ip+4 X[4] ip+5 X[5]

Arithmetic & Pointer Operator Outline 1 Pointer 2 p Arithmetic & Pointer Operator 3 Pointer Array 4 Pointer to Pointer 5 Assignment #9

Array Pointer (cont.) ตัวชี้และแถวลำดับในภาษาซีนั้น มีความใกล้ชิดกันอย่างมาก การประกาศ float v[5] เป็นการกำหนดแถวลำดับ v ขนาด 5 นั่นคือกลุ่มของวัตถุติดกัน 5 ชิ้นมีชื่อว่า v[0], v[1], v[2], v[3], v[4] v[0] v[1] v[2] v[3] v[4] 3000 3004 3008 300C 3010

Array Pointer (cont.) v[0] v[1] v[2] v[3] v[4] 3000 3004 3008 300C 3010 vPtr การประกาศ float *vPtr และกำหนดให้ vPtr ชี้ไปยังตัวแปรแถวลำดับ v สามารถทำได้สองวิธีคือ วิธีที่ 1 vPtr = v; วิธีที่ 2 vPtr = &v[0];

การกำหนด x = *vPtr คือการสำเนาค่าใน v[0] มายัง x Array Pointer (cont.) v[0] v[1] v[2] v[3] v[4] 3000 3004 3008 300C 3010 vPtr การกำหนด x = *vPtr คือการสำเนาค่าใน v[0] มายัง x

Moving Pointer Array with “+=“ and “-=“ 3000 3004 3008 300C 3010 vPtr เมื่อบวกหรือลบจำนวนเต็มกับตัวชี้แล้ว ค่าของตัวชี้มิได้เพิ่มหรือลดลงตามตัวเลขจำนวนนั้น ค่าของตัวชี้เพิ่มหรือลดตามตัวเลขจำนวนนั้นคูณกับขนาดของวัตถุที่ตัวชี้นั้นชี้อยู่ ขนาด (ไบท์) ขึ้นกับประเภทของข้อมูลที่ใช้ในวัตถุนั้น ตัวอย่าง (กำหนดขนาดของวัตถุ ชนิด float คือ 4 ไบท์) vPtr += 2; // vPtr = vPtr+(2*4) // or vPtr = 3000+(2*4)

Moving Pointer Array with “+=“ and “-=“ (cont.) 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

Moving Pointer Array with “+=“ and “-=“ (cont.) 3000 3004 3008 3012 3016 vPtr 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

Moving Pointer Array with “+“ and “-“ 3000 3004 3008 300C 3010 v[0] v[1] v[2] v[3] v[4] vPtr vPtr+2 vPtr+4 vPtr+1 vPtr+3

Calculation Element by Pointer 3000 3004 3008 300C 3010 v[0] v[1] v[2] v[3] v[4] vPtr v2Ptr vPtr = &v[0]; // vPtr = 3000 v2Ptr = &v[2]; // or v2Ptr = 3008 x = v2Ptr – vPtr; // x = ? ค่าที่ 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] 0350 pt_num pt_num = &num[1]; 0412 0418 pt_num = &num[4]; 050A temp int temp; temp = *pt_num; 907

Using Pointer with Array (cont.) 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} -13.12 0310 num[4] 19.01 0300 num[0] 26.09 0304 num[1] -4.23 0308 num[2] -4.24 030C num[3]

The Other Using Pointer with Array 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] float *pt_num; 090D pt_num pt_num = num; 0300 +3 +1 float test1, test2; 032A test1 322E test2 test1 = *(pt_num+3); -4.24 26.09 test2 = *(pt_num+1);

* คือ เครื่องหมายแสดงว่าเป็นตัวแปรประเภทตัวชี้ Pointer Array 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 notation b[0] = 10 b[1] = 20 b[2] = 30 b[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 where The pointer is the array name *(b + 0) = 10 *(b + 1) = 20 *(b + 2) = 30 *(b + 3) = 40

Using Pointer in Array Format 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 notation bPtr[0] = 10 bPtr[1] = 20 bPtr[2] = 30 bPtr[3] = 40

Arithmetic & Pointer Operator Outline 1 Pointer 2 p Arithmetic & Pointer Operator 3 Pointer Array 4 Pointer to Pointer 5 Assignment #9

* * คือ เครื่องหมายแสดงว่าเป็นตัวแปรประเภทตัวชี้ ซ้อนตัวชี้ Pointer of Pointer type **ptt_name; type คือ ชนิดของตัวแปรประเภทตัวชี้ * * คือ เครื่องหมายแสดงว่าเป็นตัวแปรประเภทตัวชี้ ซ้อนตัวชี้ ptt_name คือ ชื่อตัวแปรประเภทตัวชี้ซ้อนตัวชี้

Pointer of Pointer (cont.) float time = 9.28; 0100 time 9.28 0250 pt_time 03AA ptt_time float *pt_time; 0100 0250 float **ptt_time; pt_time = &time; ptt_time = &pt_time; float temp1; 0510 temp1 temp1 = *pt_time; 9.28 float temp2; temp2 = **ptt_time; 05C0 temp2 9.28

Example 4 #include<stdio.h> int main(void) { char **ptr = NULL;   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; }

One-dimensional arrays #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;

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; }

Arithmetic & Pointer Operator Outline 1 Pointer 2 p Arithmetic & Pointer Operator 3 Pointer Array 4 Pointer to Pointer 5 Assignment #9

Lab direction 7