งานนำเสนอกำลังจะดาวน์โหลด โปรดรอ

งานนำเสนอกำลังจะดาวน์โหลด โปรดรอ

ตัวชี้ P O I N T E R Created By Tasanawan Soonklang

งานนำเสนอที่คล้ายกัน


งานนำเสนอเรื่อง: "ตัวชี้ P O I N T E R Created By Tasanawan Soonklang"— ใบสำเนางานนำเสนอ:

1 ตัวชี้ P O I N T E R Created By Tasanawan Soonklang
Template PPT By

2 ตัวแปร เป็นชื่อที่ใช้แทนข้อมูลหนึ่งๆ
int a; a = 10; เป็นชื่อที่ใช้แทนข้อมูลหนึ่งๆ การประกาศตัวแปรเป็นการกำหนดชื่อเพื่อใช้แทนข้อมูล มีการจองเนื้อที่ในหน่วย ความจำเพื่อเก็บข้อมูล a 400 402 404 10

3 พอยเตอร์ หรือ ตัวชี้ ตัวแปรพอยเตอร์ (pointer) ทำหน้าที่เป็นตัวชี้
a 400 402 404 10 int a; a = 10; int *ptr; ptr = &a; ตัวแปรพอยเตอร์ (pointer) ทำหน้าที่เป็นตัวชี้ เก็บแอดเดรส (address) ของข้อมูล แอดเดรส คือ ตำแหน่งในหน่วยความจำ 400 ptr 800

4 ชนิดข้อมูล *ชื่อตัวแปรพอยเตอร์ ;
รูปแบบการประกาศ ชนิดข้อมูล *ชื่อตัวแปรพอยเตอร์ ; ใช้เครื่องหมาย * ตัวอย่างเช่น int *p; char *pch; การเข้าถึงข้อมูล *p = 20; *pch = ‘x’; printf(“%d”,*p); printf(“%c”,*pch);

5 ชื่อตัวแปรพอยเตอร์ = &ชื่อตัวแปรที่ต้องการชี้;
การกำหนดค่า ชื่อตัวแปรพอยเตอร์ = &ชื่อตัวแปรที่ต้องการชี้; ใช้เครื่องหมาย & คล้ายกับการใช้ scanf int a; scanf(“%d”,&a); 1 int age; 2 int *ptr; 3 age = 25; 4 ptr = &age;

6 การใช้งาน ข้อแตกต่างระหว่าง age และ &age age อ้างถึงค่าที่เก็บในตัวแปร
1 int age; 2 int *ptr; 3 age = 25; 4 ptr = &age; 5 printf(“%d”,age); ptr Value of age age Address of age 72009 25 72009

7 การใช้งาน ข้อแตกต่างระหว่าง ptr และ *ptr ptr อ้างถึงแอดเดรส
1 int age, ru=10; 2 int *ptr; 3 age = 25; 4 ptr = &age; 5 printf(“%d”,age); 6 *ptr = ru; *ptr); ptr Value of age age 72009 Address of age 25 10 72009

8 ptr 25 age 72011 72009 10 ru 10 25 10 1 int age, ru=10; 2 int *ptr;
5 printf(“%d”,*ptr); 6 *ptr = ru; 7 printf(“%d”,*ptr); 25 10 72009 ptr 25 age 72011 72009 10 72011 1 int age, ru=10; 2 int *ptr; 3 age = 25; 4 ptr = &age; 5 printf(“%d”,*ptr); 6 ptr = &ru; 7 printf(“%d”,*ptr); ru 10

9 100 #include<stdio.h> main() { int *p,q; q = 100; p = &q;
printf("%d", *p); } #include<stdio.h> main() { int *p; float q, temp; temp = ; p = &temp; q = *p; printf("%f",q); } can’t convert float to int

10 Pointer & Array พอยเตอร์และอาร์เรย์มีความสัมพันธ์กัน ชื่อตัวแปรอาร์เรย์จะเก็บแอดเดรสอยู่แล้ว สามารถใช้พอยเตอร์ชี้แทนการระบุตำแหน่ง (index) ได้ ทำงานได้รวดเร็วกว่า โดยเฉพาะเมื่อข้อมูลมีขนาดใหญ่ พอยเตอร์จะชี้ไปยังตำแหน่งแรกของอาร์เรย์ อ้างถึงสมาชิกตัวใดๆ ได้โดยการกระทำทางคณิตศาสตร์ โดยใช้เครื่องหมาย ++, --, +, -

11 ตัวอย่าง x 1 400 2 3 404 ptr 400 402 404 ptr+1 อ้างถึงแอดเดรสของ x[1]
1 int x[5] = {1,2,3,4,5}; 2 int *ptr; 3 ptr = x; 4 ptr = &x[2]; 1 400 2 3 X[2] 404 ptr+1 อ้างถึงแอดเดรสของ x[1] ptr+2 อ้างถึงแอดเดรสของ x[2] *(ptr+2) อ้างถึงค่าของ x[2] ptr 800

12 1 2 3 Pointers are fun #include "stdio.h"
#include "stdio.h" int a[10] = {1,2,3,4,5,6,7,8,9,10}; main() { int *p; p = a; printf("%d %d %d\n",*p, *(p+1), *(p+2)); printf("%d %d %d",a[0],a[1],a[2]); } Pointers are fun #include "stdio.h" char str[ ] = "Pointers are fun"; main() { char *p; int i; p = str; for(i=0;p[i];i++) printf("%c",p[i]); }

13 printf("Enter a string :"); gets(str); for(i=0;str[i];i++)
Enter a string: ThAiLaNd THAILAND thailand #include "stdio.h" #include "ctype.h" main() { char str[80] ; int i; printf("Enter a string :"); gets(str); for(i=0;str[i];i++) str[i] = toupper(str[i]); printf("%s\n",str); str[i] = tolower(str[i]); } char *p; p = str; while(*p) { *p = toupper(*p); p++; } printf("%s\n",str); while(*p) *p++ = tolower(*p);

14 char str1[] = "Pointers are fun"; main() { char str2[80], *p1, *p2;
Nuf era sretnioP #include "stdio.h" #include "string.h" char str1[] = "Pointers are fun"; main() { char str2[80], *p1, *p2; p1 = str1+strlen(str1)-1; p2 = str2; while(p1>=str1) *p2 = *p1; *p1--; *p2++; } printf("%s\n%s",str1,str2); *p2++ = *p1--;

15 #include <stdio.h> #define n 5
char days[ ][10] = {"monday","tuesday","wednesday", "thursday", "friday""}; void print_strings(char *table[ ], int num); main() { print_strings(days,n); } void print_strings(char *table[ ], int num) int i; for (i = 0; i < num; i++) printf(“%d %s\n", table[i], table[i]); 0000 monday 0007 tuesday 0015 wednesday 0025 thursday 0034 friday *days[ ] *(days + i) *(days+0) -> days[0] = “monday” *(days+1) -> days[1] = “tuesday” *(days+2) -> days[2] = “wednesday” *(days+3) -> days[3] = “thursday” *(days+4) -> days[4] = “friday”

16 days[0] days[1] days[2] days[3] days[4] days[0] days[0]+0 *(days+0)

17 Lab 10 ใช้ index ในการอ้างถึงค่า
Enter string1: Computer science Enter string2: Information technology Swap strings String1 = Information technology String2 = Computer science Lab 10 ใช้ index ในการอ้างถึงค่า เขียนโปรแกรมเก็บค่าสตริง 2 ค่าขนาดไม่เกิน 30 ตัวอักษรในอาร์เรย์ A และ B ทำการสลับค่าข้อมูลในตัวแปรอาร์เรย์ทั้งสอง แสดงผลในค่าอาร์เรย์ทั้งสอง ใช้ pointer ในการอ้างถึงค่า เขียนโปรแกรมเก็บค่าสตริง 2 ค่าไม่ระบุขนาดโดยใช้พอยเตอร์ A และ B ทำการสลับค่าข้อมูลในตัวแปรพอยเตอร์ทั้งสอง แสดงผลในค่าพอยเตอร์ทั้งสอง

18 Lab 10 1. กำหนดตัวแปรสตริงเพื่อเก็บข้อความขนาดไม่เกิน 80 คำ
Homework Lab 10 Enter string: The representation of objects of the type is hidden from the program units. 15 words Words Length The 3 representation 14 of 2 objects 7 … ... 1. กำหนดตัวแปรสตริงเพื่อเก็บข้อความขนาดไม่เกิน 80 คำ 2. ตรวจสอบสตริงว่ามีกี่คำ 3. เก็บแต่ละคำในอาร์เรย์ของตัวแปรสตริงแบบพอยเตอร์ 4. แสดงผลลัพท์โดยพิมพ์ จำนวนคำทั้งหมด คำ และ ความยาวของคำ

19 pointer age 72009 50 18 #include<stdio.h> void main() { int age;
How old are you: 18 You are 18 years old. You are 50 years old. #include<stdio.h> void main() { int age; int *pointer; printf(“How old are you : ”); scanf(“%d”,&age); printf(“You are %d years old.\n”,age); pointer = &age; *pointer =50; printf(“You are %d years old. \n, age); } pointer Value of age age 72009 50 18 Address of age 72009

20 Pointer & Function ใช้ส่งผ่านค่าไปยังฟังก์ชั่นได้ ใช้ในการคืนค่ากลับจากฟังก์ชั่นได้มากกว่า 1 ค่า ใช้ในการรับส่งค่าอาร์เรย์ให้กับฟังก์ชั่นได้ เรียกว่า call-by-reference

21 Call-by-value tmp = p; p = q; q = tmp; สลับค่า (swap)
a = 6, b = 2 #include "stdio.h“ void swap (int p, int q) { int tmp; tmp = p; p = q; q = tmp; } main() int a = 6, b = 2; printf("a = %d, b = %d\n", a, b); swap(a, b);

22 Call-by-reference #include "stdio.h“ void swap (int *p, int *q) {
a = 6, b = 2 a = 2, b = 6 #include "stdio.h“ void swap (int *p, int *q) { int tmp; tmp = *p; *p = *q; *q = tmp; } main() int a = 6, b = 2; printf("a = %d, b = %d\n", a, b); swap(&a, &b);

23 printf("a = %d, b = %d\n", a, b); swap(&a, &b); }
#include "stdio.h“ void swap (int *, int *) main() { int a = 6, b = 2; printf("a = %d, b = %d\n", a, b); swap(&a, &b); } void swap (int *p, int *q) int tmp; tmp = *p; *p = *q; *q = tmp;

24 #include<stdio.h> void change(int p) { p = 50;
Before num = 20 Now num = 50 After num = 20 Before num = 20 Now num = 50 After num = 50 #include<stdio.h> void change(int p) { p = 50; printf(“Now num = %d\n”, p); } void main() int num=20; printf(“Before num = %d\n”,num); change(num); printf(“After num = %d\n”, num); * * * change(&num);

25 #include<stdio.h> int num1, num2; void input() {
Input number1 : 15 Input number2 : 30 #include<stdio.h> int num1, num2; void input() { printf(“\n Input number1 : ”); scanf(“%d”,&num1); printf(“\n Input number2 : ”); scanf(“%d”,&num2); } void main() input(num1,num2); printf(“%d %d”, num1,num2);

26 #include<stdio.h> void input(int a, int b) {
Input number1 : 15 Input number2 : 30 #include<stdio.h> void input(int a, int b) { printf(“\n Input number1 : ”); scanf(“%d”, a); printf(“\n Input number2 : ”); scanf(“%d”, b); } void main() int num1, num2; input( num1, num2); printf(“%d %d”, num1,num2); * * & & & &

27 #include<stdio.h>
void input(int *, int *); int adder(int a, int b) { return a+b; } void main() int num1, num2,result; input(&num1,&num2); result = add(num1, num2); printf(“%d”, result); void adder(int a, int b, int *add) { *add = a+b; } add(num1, num2,result);

28 May the Force be with YOU.
…^( ^ ^ )^... Thanks, your watching.


ดาวน์โหลด ppt ตัวชี้ P O I N T E R Created By Tasanawan Soonklang

งานนำเสนอที่คล้ายกัน


Ads by Google