9 1 01006012 Computer Programming Address 0000 0001

  • Slides: 34
Download presentation

9. 1 โครงสรางของหนวยความจำ และตวช 01006012 Computer Programming ทอยของขอมล )Address) 0000 0001 0002 0003 0004

9. 1 โครงสรางของหนวยความจำ และตวช 01006012 Computer Programming ทอยของขอมล )Address) 0000 0001 0002 0003 0004 0005 0006 ประเภท ชอตวแปร 15 int count 2 bytes ขนาด 3. 1415 float pi 4 bytes 'A' char ch 1 bytes … … XXXX ขอมล 2

9. 1 โครงสรางของหนวยความจำ และตวช 01006012 Computer Programming ทอยของขอมล )Address) 0000 0001 0002 0003 0004

9. 1 โครงสรางของหนวยความจำ และตวช 01006012 Computer Programming ทอยของขอมล )Address) 0000 0001 0002 0003 0004 0005 0006 ประเภท ชอตวแปร 15 int count 2 bytes ขนาด 3. 1415 float pi 4 bytes 'A' char ch 1 bytes … … XXXX ขอมล 0000 ptr = &count ตวช ptr ชไปทอยของ ตวแปรชอ count 3

. 91. 2 ตวดำเนนการ (Reference Operator) “&” 01006012 Computer Programming ตวดำเนนการ & (Referencing Operator)

. 91. 2 ตวดำเนนการ (Reference Operator) “&” 01006012 Computer Programming ตวดำเนนการ & (Referencing Operator) ตวดำเนนการ & สงกลบเลขทอย (Address) ของวตถ XXXX 15 int XXXX int ptr = &count; /* ptr count; *ptr; มคาเปน XXXX */ 5

. 91. 2 ตวดำเนนการ (Reference Operator) “&” 01006012 Computer Programming ทอยของขอมล )Address) ขอมล ประเภท

. 91. 2 ตวดำเนนการ (Reference Operator) “&” 01006012 Computer Programming ทอยของขอมล )Address) ขอมล ประเภท ชอตวแปร 0000 15 int count 2 bytes 0002 3. 1415 float pi 4 bytes 0006 'A' char ch 1 bytes 0006 char *ptr_char = &ch; 0002 float *ptr_float = π 0000 int *ptr_int = &count; ขนาด 6

โปรแกรม 9. 1 โปรแกรมแสดงขอมลผานทาง pointer 01006012 Computer Programming #include<stdio. h> #include<conio. h> int main()

โปรแกรม 9. 1 โปรแกรมแสดงขอมลผานทาง pointer 01006012 Computer Programming #include<stdio. h> #include<conio. h> int main() { letter num point char letter = 'D'; D 19 26. 09 int num = 19; 0000 0002 0004 float point = 26. 09; char *pt_letter; int *pt_num; pt_letter pt_num pt_point float *pt_point; 0000 0002 0004 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; } 9

โปรแกรม 9. 1 โปรแกรมแสดงขอมลผานทาง pointer 01006012 Computer Programming letter num Address of letter =

โปรแกรม 9. 1 โปรแกรมแสดงขอมลผานทาง pointer 01006012 Computer Programming letter num Address of letter = 0000 Address of num = 0002 Address of point = 0004 point D 19 26. 09 0000 0002 0004 pt_letter pt_num pt_point 0000 0002 0004 ? ? ? ? 10

โปรแกรม 9. 2 โปรแกรมการใช referencing และ dereferencing 01006012 Computer Programming int main() num 1

โปรแกรม 9. 2 โปรแกรมการใช referencing และ dereferencing 01006012 Computer Programming int main() num 1 price 1 hint 1 { 113 4. 85 J int num 1 = 113, num 2; float price 1 = 4. 85; --x-- --y-- --z-char hint 1 = 'J', hint 2; int *pt_num; pt_num pt_price pt_hint float *pt_price; --x---y---z-char *pt_hint; pt_num = &num 1; pt_price = &price 1; num 2 hint 2 pt_hint = &hint 1; num 2 = *pt_num; 113 J hint 2 = *pt_hint; printf ("Variable num 1 = %d n", num 2); printf ("Variable price 1 = %f n", *pt_price); printf ("Variable hint 2 = %c n", hint 2); return 0; } 11

โปรแกรม 9. 2 โปรแกรมการใช referencing & dereferencing 01006012 Computer Programming num 1 price 1

โปรแกรม 9. 2 โปรแกรมการใช referencing & dereferencing 01006012 Computer Programming num 1 price 1 hint 1 Variable num 1 = 113 Variable price 1 = 4. 850000 Variable hint 2 = J 113 4. 85 J --x-- --y-- --z-pt_num pt_price pt_hint --x-- --y-- --z-- num 2 hint 2 113 J 12

โปรแกรม 9. 3 โปรแกรมการใช referencing และ dereferencing แบบ 2 01006012 Computer Programming #include<stdio. h>

โปรแกรม 9. 3 โปรแกรมการใช referencing และ dereferencing แบบ 2 01006012 Computer Programming #include<stdio. h> #include<conio. h> int main() { int a; /* a is an integer */ int *a. Ptr; /* a. Ptr is a pointer to an integer */ a = 7; a. Ptr = &a; /* a. Ptr set to address of a */ printf ("The address of a is %pn" "The value of a. Ptr is %pnn", &a, a. Ptr); printf ("The value of a is %dn" "The value of *a. Ptr is %dnn", a, *a. Ptr); printf ("Proving that * and & are complements of " "each other. n&*a. Ptr = %pn*&a. Ptr = %pn", &*a. Ptr, *&a. Ptr); return 0; 13 }

โปรแกรม 9. 3 โปรแกรมการใช referencing และ dereferencing แบบ 2 01006012 Computer Programming FFF 4

โปรแกรม 9. 3 โปรแกรมการใช referencing และ dereferencing แบบ 2 01006012 Computer Programming FFF 4 7 a ? ? FFF 4 a. Ptr printf ("The value a is %dn" address of a is ("Proving *an and &%pn" are complements of " int a; /*that aof is integer */ The address of a is FFF 4 "The of *a. Ptr is %dnn", a, = *a. Ptr); "eachvalue other. n&*a. Ptr =%pnn", %pn*&a. Ptr %pn", */ int *a. Ptr; /* a. Ptr is ais pointer to &a, an integer The value of a. Ptr is FFF 4 a = 7; &*a. Ptr, *&a. Ptr); The value ofof a is 7 a. Ptr address a/*is FFF 4 set to address of a */ a. Ptr = &a; The value of *a. Ptr a and is is 7 is Proving that * & FFF 4 are The of 7 complements of each other. a. Ptr The value of *a. Ptr is 7 &*a. Ptr = FFF 4 *&a. Ptr = FFF 4 Proving that * and & are complements of each other. &*a. Ptr = FFF 4 *&a. Ptr = FFF 4 14

9. 3 Pointer ซอน Pointer float time = 9. 28; float *pt_time; float **ptt_time;

9. 3 Pointer ซอน Pointer float time = 9. 28; float *pt_time; float **ptt_time; pt_time = &time; 01006012 Computer Programming time 9. 28 0100 pt_time ptt_time 0100 0250 03 AA ptt_time = &pt_time; float temp 1; temp 1 = *pt_time; temp 1 float temp 2; 9. 28 0510 temp 2 = **ptt_time; temp 2 9. 28 05 C 0 17

9. 4. 1 การยาย Pointer ทชตวแปรอารเรย ดวย += และ -= 01006012 Computer Programming v.

9. 4. 1 การยาย Pointer ทชตวแปรอารเรย ดวย += และ -= 01006012 Computer Programming v. Ptr 3000 3004 3008 300 C 3010 v[0] v[1] v[2] v[3] v[4] • v. Ptr += 2; // v. Ptr = v. Ptr+(2*4) // or v. Ptr = 3000+(2*4) • หลงจากคำสงขางตนแลว v. Ptr จะชไปท v[2] v. Ptr 3000 3004 3008 300 C 3010 v[0] v[1] v[2] v[3] v[4] 23

9. 4. 1 การยาย Pointer ทชตวแปรอารเรย ดวย += และ -= 01006012 Computer Programming v.

9. 4. 1 การยาย Pointer ทชตวแปรอารเรย ดวย += และ -= 01006012 Computer Programming v. Ptr 3000 3004 3008 3012 3016 v[0] v[1] v[2] v[3] v[4] • v. Ptr -= 2; // v. Ptr = v. Ptr-(2*4) // or v. Ptr = 3008 -(2*4) • หลงจากคำสงขางตนแลว v. Ptr จะชไปท v[0] v. Ptr 3000 3004 3008 3012 3016 v[0] v[1] v[2] v[3] v[4] 24

9. 4. 2 การยาย Pointer ทชตวแปรอารเรย ดวย + และ - 01006012 Computer Programming 3000

9. 4. 2 การยาย Pointer ทชตวแปรอารเรย ดวย + และ - 01006012 Computer Programming 3000 3004 3008 300 C 3010 v[0] v[1] v[2] v[3] v[4] v. Ptr+2 v. Ptr+1 v. Ptr+4 v. Ptr+3 25

โปรแกรม 9. 4 โปรแกรมแสดงการใช pointer กบ array 01006012 Computer Programming int num[5] = {12,

โปรแกรม 9. 4 โปรแกรมแสดงการใช pointer กบ array 01006012 Computer Programming int num[5] = {12, 34, 112, 45, 907}; *pt_num; num[0] num[1] num[2] num[3] num[4] 12 34 112 45 907 0410 0412 0414 0416 0418 pt_num = &num[1]; pt_num = &num[4]; int temp; temp = *pt_num; pt_num 0412 0418 0350 temp 907 050 A 27

01006012 Computer Programming type name[10]; type *pt_name; pt_name = name; //pt_name = &name[0] float

01006012 Computer Programming 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} num[0] num[1] num[2] num[3] num[4] 19. 01 26. 09 -4. 23 -4. 24 -13. 12 0300 0304 0308 030 C 0310 28

โปรแกรม 9. 5 โปรแกรมแสดงการใช pointer กบ array 01006012 Computer Programming float *pt_num; num[0] num[1]

โปรแกรม 9. 5 โปรแกรมแสดงการใช pointer กบ array 01006012 Computer Programming float *pt_num; num[0] num[1] num[2] num[3] num[4] 19. 01 26. 09 -4. 23 -4. 24 -13. 12 0300 0304 0308 030 C 0310 pt_num = num; float test 1, test 2; test 1 = *(pt_num+3); test 2 = *(pt_num+1); 0300 +3 090 D test 1 test 2 -4. 24 26. 09 032 A 322 E +1 29

9. . 15กรณท 1 ใช array ปกต 01006012 Computer Programming int main() { int

9. . 15กรณท 1 ใช array ปกต 01006012 Computer Programming int main() { int i, offset, b[] = {10, 20, 30, 40}; int *b. Ptr = b; /* set b. Ptr to point to array b */ printf("Array b printed with: n" "Array subscript notationn"); for (i=0; i<=3; i++) printf("b[%d] = %dn", i, b[i]); return 0; } Array b printed with: Array subscript notation b[0] = 10 b[1] = 20 b[2] = 30 b[3] = 40 31

9. . 25กรณท 2 ใช array เขยนแบบ pointer 01006012 Computer Programming int main() {

9. . 25กรณท 2 ใช array เขยนแบบ pointer 01006012 Computer Programming int main() { int i, offset, b[] = {10, 20, 30, 40}; int *b. Ptr = b; /* set b. Ptr to point to array b */ printf("Pointer/offset notation wheren" "the pointer is the array namen"); for (offset = 0; offset<=3 ; offset++) printf("*(b + %d) = %dn", 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 32

9. 5. 3 กรณท 3 ใช pointer เขยนแบบ array 01006012 Computer Programming int main()

9. 5. 3 กรณท 3 ใช pointer เขยนแบบ array 01006012 Computer Programming int main() { int i, offset, b[] = {10, 20, 30, 40}; int *b. Ptr = b; /* set b. Ptr to point to array b */ printf("Pointer subscript notationn"); for (i=0 ; i<=3 ; i++) printf("b. Ptr[%d] = %dn", i, b. Ptr[i]); return 0; } Pointer b. Ptr[0] b. Ptr[1] b. Ptr[2] b. Ptr[3] subscript notation = 10 = 20 = 30 = 40 33

9. 5. 4 กรณท 4 ใช pointer ปกต 01006012 Computer Programming int main() {

9. 5. 4 กรณท 4 ใช pointer ปกต 01006012 Computer Programming int main() { int i, offset, b[] = {10, 20, 30, 40}; int *b. Ptr = b; /* set b. Ptr to point to array b */ printf("Pointer/offset notationn"); for (offset=0; offset<=3; offset++) printf("*(b. Ptr + %d) = %dn", offset, *(b. Ptr + offset)); return 0; } Pointer/offset notation *(b. Ptr + 0) = 10 *(b. Ptr + 1) = 20 *(b. Ptr + 2) = 30 *(b. Ptr + 3) = 40 34