Address 0000 0001 0002 0003 0004 0005 0006

  • Slides: 45
Download presentation

โครงสรางของหนวยความจำ ทอยของขอ มล )Address) 0000 0001 0002 0003 0004 0005 0006 … Jirasak Sittigorn

โครงสรางของหนวยความจำ ทอยของขอ มล )Address) 0000 0001 0002 0003 0004 0005 0006 … Jirasak Sittigorn http: //www. kmitl. ac. th/~ksjirasa/ 15 ประเภท ชอตว แปร ขนาด int count 2 bytes 3. 1415 float pi 4 bytes 'A' char ch 1 bytes … … … XXXX ขอมล 01001012 Principle of Computer Programming 3 King Mongkut's Institute of Technology

ตวช (Pointers) ทอยของขอ มล )Address) 0000 0001 0002 0003 0004 0005 0006 … Jirasak

ตวช (Pointers) ทอยของขอ มล )Address) 0000 0001 0002 0003 0004 0005 0006 … Jirasak Sittigorn http: //www. kmitl. ac. th/~ksjirasa/ ขอมล 15 ประเภท ชอตว แปร ขนาด int count 2 bytes 3. 1415 float pi 4 bytes 'A' char ch 1 bytes … … … XXXX 01001012 Principle of Computer Programming 0000 ptr = &count ตวช ptr ชไปทอยขอ 4 ง King Mongkut's Institute of Technology

การประกาศตวช type 01001012 Principle of Computer Programming *pointer_name type (pointer) คอ ชนดของตวแปรประเภทตวช คอ เครองหมายแสดงวาเปนตวแปรประเภทตวช

การประกาศตวช type 01001012 Principle of Computer Programming *pointer_name type (pointer) คอ ชนดของตวแปรประเภทตวช คอ เครองหมายแสดงวาเปนตวแปรประเภทตวช pointer_name คอ ชอของตวแปรประเภทตวช int *ptr_int; /* pointer to integer */ float *ptr_float; /* pointer to float */ ตวอยางการประกาศตวช char *ptr_char; /* pointer to char */ * Jirasak Sittigorn http: //www. kmitl. ac. th/~ksjirasa/ 5 King Mongkut's Institute of Technology

ตวดำเนนการ & (สงคาทอย ) 01001012 Principle of Computer Programming &variable • ตวดำเนนการ & ใชสำหรบสงคาทอย

ตวดำเนนการ & (สงคาทอย ) 01001012 Principle of Computer Programming &variable • ตวดำเนนการ & ใชสำหรบสงคาทอย (Address) ของตวแปร หรอ วตถ ตวอยางการใชงานตวดำเนนการ 21 EC 21 ED 15 & int count int *ptr int count, *ptr; ptr = &count; /* ptr 21 EC */ Jirasak Sittigorn http: //www. kmitl. ac. th/~ksjirasa/ เกบตำแหนงของ count มคาเปน 6 King Mongkut's Institute of Technology

ตวอยางการใชตวชเกบตำแหนงตวแปร 01001012 Principle of Computer Programming ทอยของขอ มล )Address) ขอมล ประเภท int ชอตว แปร

ตวอยางการใชตวชเกบตำแหนงตวแปร 01001012 Principle of Computer Programming ทอยของขอ มล )Address) ขอมล ประเภท int ชอตว แปร count ขนาด 2 bytes 0000 15 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; Jirasak Sittigorn http: //www. kmitl. ac. th/~ksjirasa/ 7 King Mongkut's Institute of Technology

ตวดำเนนการ * (อางอง ) 01001012 Principle of Computer Programming *pointer_name • ตวดำเนนการ * เปน

ตวดำเนนการ * (อางอง ) 01001012 Principle of Computer Programming *pointer_name • ตวดำเนนการ * เปน ตวดำเนนการเชงออม 21 CE (Indirection) หรอ กลบการอางอง (Dereferencing) 21 CF ตวอยางการใชงานตวดำเนนการ * 21 D 0 21 D 1 21 D 2 21 D 3 21 D 4 21 D 5 z[0] z[1] z[2] int count = 15, y, z[10]; int *ptr; */ ptr เปนตวช ��int */ ptr 21 E 2 ptr = &count; */ptr ชไปท ��count 21 E 6 */ y 21 E 7 y =* ptr; */y มคาเทากบคาท 21 E 8 15 count 21 E 9 21 E 8/* Jirasak Sittigorn 8 *ptr = 0; */ คาท 21 E 8 เปน 0 King Mongkut's Institute of Technology http: //www. kmitl. ac. th/~ksjirasa/

ตวอยางโปรแกรมการใช #include<stdio. h> int main() { printf กบตวช (1) 01001012 Principle of Computer Programming

ตวอยางโปรแกรมการใช #include<stdio. h> int main() { printf กบตวช (1) 01001012 Principle of Computer Programming letter num point char letter = 'D'; D 19 26. 09 int num = 19; float point = 26. 09; 243 F 243 C 2438 char * pt_letter; int * pt_num; pt_letter pt_num pt_point float * pt_point; pt_letter = & letter; 243 F 243 C 2438 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; } Jirasak Sittigorn http: //www. kmitl. ac. th/~ksjirasa/ 10 King Mongkut's Institute of Technology

ตวอยางโปรแกรมการใช printf กบตวช • ผลลพธการรนโปรแกรม Address of letter = 2523: 243 F Address of

ตวอยางโปรแกรมการใช printf กบตวช • ผลลพธการรนโปรแกรม Address of letter = 2523: 243 F Address of num = 2523: 243 C Address of point = 2523: 2438 (2) 01001012 Principle of Computer Programming letter num D 19 point 26. 09 243 F 243 C 2438 pt_letter pt_num pt_point Jirasak Sittigorn http: //www. kmitl. ac. th/~ksjirasa/ 243 F 243 C 2438 ? ? ? ? 11 King Mongkut's Institute of Technology

ตวอยางโปรแกรมการใช - A (1) referencing และ dereferencing 01001012 Principle of Computer Programming int main()

ตวอยางโปรแกรมการใช - A (1) referencing และ dereferencing 01001012 Principle of 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; Jirasak } Sittigorn http: //www. kmitl. ac. th/~ksjirasa/ 12 King Mongkut's Institute of Technology

ตวอยางโปรแกรมการใช - A (2) • ผลลพธการรนโปรแกรม Variable num 1 = 113 Variable price 1

ตวอยางโปรแกรมการใช - A (2) • ผลลพธการรนโปรแกรม Variable num 1 = 113 Variable price 1 = 4. 850000 Variable hint 2 = J referencing และ dereferencing 01001012 Principle of Computer Programming num 1 price 1 hint 1 113 4. 85 --x-- --y-- --z-pt_num pt_price pt_hint --x-- Jirasak Sittigorn http: //www. kmitl. ac. th/~ksjirasa/ J --y-- --z-- num 2 hint 2 113 J 13 King Mongkut's Institute of Technology

ตวอยางโปรแกรมการใช - B (1) referencing และ dereferencing 01001012 Principle of Computer Programming #include<stdio. h>

ตวอยางโปรแกรมการใช - B (1) referencing และ dereferencing 01001012 Principle of Computer Programming #include<stdio. 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; } Jirasak Sittigorn http: //www. kmitl. ac. th/~ksjirasa/ 14 King Mongkut's Institute of Technology

ตวอยางโปรแกรมการใช - B (2) • ผลลพธการรนโปรแกรม FFF 4 7 a referencing และ dereferencing 01001012

ตวอยางโปรแกรมการใช - B (2) • ผลลพธการรนโปรแกรม FFF 4 7 a referencing และ dereferencing 01001012 Principle of Computer Programming ? ? 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 Jirasak Sittigorn http: //www. kmitl. ac. th/~ksjirasa/ 15 King Mongkut's Institute of Technology

สรปการดำเนนการของตวแปรประเภทตวช 01001012 Principle of Computer Programming การประกาศตวแปรพอยนเต type *อรpt_name; variable 1 แสดงตำแหนงขอมลด pt_name =วย&variable

สรปการดำเนนการของตวแปรประเภทตวช 01001012 Principle of Computer Programming การประกาศตวแปรพอยนเต type *อรpt_name; variable 1 แสดงตำแหนงขอมลด pt_name =วย&variable 1; & pt_name แสดงขอมลดวย * variable 2 = *pt_name; Jirasak Sittigorn http: //www. kmitl. ac. th/~ksjirasa/ d 1906 ---variable 2 d ---16 King Mongkut's Institute of Technology

พอยนเตอรซอนพอยนเตอร (Nested Pointer) (2) 01001012 Principle of Computer Programming ตวอยางการใชงานพอยนเตอรซอนพอยนเตอร float time = 9.

พอยนเตอรซอนพอยนเตอร (Nested Pointer) (2) 01001012 Principle of Computer Programming ตวอยางการใชงานพอยนเตอรซอนพอยนเตอร float time = 9. 28; float *pt_time; float **ptt_time; time pt_time ptt_time 9. 28 0100 0250 03 AA pt_time = &time; ptt_time = &pt_time; float temp 1; temp 1 = *pt_time; 9. 28 0510 float temp 2; temp 2 = **ptt_time; Jirasak Sittigorn http: //www. kmitl. ac. th/~ksjirasa/ temp 2 9. 28 05 C 0 18 King Mongkut's Institute of Technology

เลขคณตของตวช (3) 01001012 Principle of Computer Programming v[0] v[1] v[2] v[3] v[4] 2 3

เลขคณตของตวช (3) 01001012 Principle of Computer Programming v[0] v[1] v[2] v[3] v[4] 2 3 5 7 11 3000 3004 3008 300 C 3010 Object Address v. Ptr float v[5]={2, 3, 5, 7, 11}; ; float* v. Ptr; v. Ptr = &v[0]; v. Ptr += 2; /* v. Ptr = v. Ptr+2 */ v. Ptr += 1; /* v. Ptr = v. Ptr+1 */ v. Ptr -= 3; /* v. Ptr = v. Ptr+3 */ Jirasak Sittigorn http: //www. kmitl. ac. th/~ksjirasa/ 24 King Mongkut's Institute of Technology

เลขคณตของตวช v[0] 3000 (4) v[1] 3004 v. Ptr v[2] 3008 01001012 Principle of Computer

เลขคณตของตวช v[0] 3000 (4) v[1] 3004 v. Ptr v[2] 3008 01001012 Principle of Computer Programming v[3] 300 C v. Ptr+2 Jirasak Sittigorn http: //www. kmitl. ac. th/~ksjirasa/ v[4] 3010 Object Address v. Ptr+4 25 King Mongkut's Institute of Technology

เลขคณตของตวช • เมอทำการบวก (5) หรอลบ ระหวางตวช คาทไดรบคอจำนวนหนวย v[0] v[1] ของตวแปรแถวลำดบ ตวอยาง 01001012 Principle of

เลขคณตของตวช • เมอทำการบวก (5) หรอลบ ระหวางตวช คาทไดรบคอจำนวนหนวย v[0] v[1] ของตวแปรแถวลำดบ ตวอยาง 01001012 Principle of Computer Programming 2 3000 3 3004 v[2] v[3] v[4] 5 7 11 3008 float v[5]={2, 3, 5, 7, 11}; float* v 1 Ptr, *v 2 Ptr; int x; v 1 Ptr = &v[0]; // 3000 v 2 Ptr = &v[2]; // 3008 x = v 2 Ptr – v 1 Ptr; printf ("v 2 Ptr – v 1 Ptr = %d", x); Jirasak Sittigorn http: //www. kmitl. ac. th/~ksjirasa/ (element) 300 C 3010 v 1 Ptr Object Address v 2 Ptr 26 King Mongkut's Institute of Technology

ตวอยางการใชงานตวแปรพอยนเตอร กบอารเรย (1) 01001012 Principle of Computer Programming int num[5] = {12, 34, 112,

ตวอยางการใชงานตวแปรพอยนเตอร กบอารเรย (1) 01001012 Principle of 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 Jirasak Sittigorn http: //www. kmitl. ac. th/~ksjirasa/ 28 King Mongkut's Institute of Technology

ตวอยางการใชงานตวแปรพอยนเตอร กบอารเรย (2) 01001012 Principle of Computer Programming type name[10]; type *pt_name; pt_name =

ตวอยางการใชงานตวแปรพอยนเตอร กบอารเรย (2) 01001012 Principle of 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 Jirasak Sittigorn http: //www. kmitl. ac. th/~ksjirasa/ 29 King Mongkut's Institute of Technology

ตวอยางการใชงานตวแปรพอยนเตอร กบอารเรย (3) 01001012 Principle of Computer Programming float *pt_num; num[0] num[1] num[2] num[3]

ตวอยางการใชงานตวแปรพอยนเตอร กบอารเรย (3) 01001012 Principle of 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); Jirasak Sittigorn http: //www. kmitl. ac. th/~ksjirasa/ 0300 +3 090 D test 1 test 2 -4. 24 26. 09 032 A 322 E +1 30 King Mongkut's Institute of Technology

ตวอยางโปรแกรมการใช Pointers กบ Arrays (1) 01001012 Principle of Computer Programming int main() { int

ตวอยางโปรแกรมการใช Pointers กบ Arrays (1) 01001012 Principle of 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 Jirasak Sittigorn http: //www. kmitl. ac. th/~ksjirasa/ 31 King Mongkut's Institute of Technology

ตวอยางโปรแกรมการใช Pointers กบ Arrays (2) 01001012 Principle of Computer Programming int main() { int

ตวอยางโปรแกรมการใช Pointers กบ Arrays (2) 01001012 Principle of 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 Jirasak Sittigorn http: //www. kmitl. ac. th/~ksjirasa/ 32 King Mongkut's Institute of Technology

ตวอยางโปรแกรมการใช Pointers กบ Arrays (3) 01001012 Principle of Computer Programming int main() { int

ตวอยางโปรแกรมการใช Pointers กบ Arrays (3) 01001012 Principle of 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 Jirasak Sittigorn http: //www. kmitl. ac. th/~ksjirasa/ 33 King Mongkut's Institute of Technology

ตวอยางโปรแกรมการใช Pointers กบ Arrays (4) 01001012 Principle of Computer Programming int main() { int

ตวอยางโปรแกรมการใช Pointers กบ Arrays (4) 01001012 Principle of 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 Jirasak Sittigorn http: //www. kmitl. ac. th/~ksjirasa/ 34 King Mongkut's Institute of Technology

ตวอยางโปรแกรมการใช Pointers กบ Arrays (5) 01001012 Principle of Computer Programming int main() { int

ตวอยางโปรแกรมการใช Pointers กบ Arrays (5) 01001012 Principle of Computer Programming int main() { int b[10]={0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; int a[10], i, *pt. A, *pt. B; for (i=0; i<10; i++) printf ("%d ", b[i]); a[15] = 100; printf ("nn. Define a[15] = 100"); printf ("na[15] = %dnn", a[15]); for (i=0; i<10; i++) printf ("%d ", b[i]); pt. A = a; pt. B = b; printf ("nn. Address a[0] : %pn", pt. A); printf ("Address b[0] : %pn", pt. B); printf ("Address a[15] : %pn", pt. A+15); printf ("Address b[5] : %pn", pt. B+5); } a[0] 0 2240 b[5] b[0] 2242 2244 2246 2248 224 A Jirasak Sittigorn http: //www. kmitl. ac. th/~ksjirasa/ 224 C 1 2 3 4 5 6 7 8 9 2250 2254 2258 225 C 2260 2264 224 E 2252 2256 225 A 225 E 2262 2266 35 King Mongkut's Institute of Technology

a. Ptr struct card { char face[6]; // 2, 3, . . , 9,

a. Ptr struct card { char face[6]; // 2, 3, . . , 9, Jack, Queen, King, Ace char suit[8]; // space, heart, diamond, club }; struct card a; struct card *a. Ptr; strcpy(a. face, "Ace"); strcpy(a. suit, "heart"); a. Ptr = &a; printf("face : %sn", a. face); printf("face : %sn", a. Ptr->face); printf("suit : %sn", a. suit); printf("suit : %sn", a. Ptr->suit); printf ("Address of a. face : %pn", &a. face); printf ("Address of a. suit : %pn", &a. suit); Jirasak Sittigorn http: //www. kmitl. ac. th/~ksjirasa/ a A c e h e a r t a. face • เมอสมาชกในโครงสรางไมใชตวช (2) 01001012 Principle of Computer Programming a. suit ตวชกบตวแปรแบบโครงสราง 37 King Mongkut's Institute of Technology

 • เมอสมาชกในโครงสรางเปนตวช (3) 01001012 Principle of Computer Programming a. Ptr struct card {

• เมอสมาชกในโครงสรางเปนตวช (3) 01001012 Principle of Computer Programming a. Ptr struct card { char * face; // 2, 3, . . , 9, Jack, Queen, King, Ace char * suit; // space, heart, diamond, club }; A h struct card a; c e struct card *a. Ptr; e a a. face = "Ace"; r a. suit = "heart"; t a. Ptr = &a; printf("face : %sn", a. face); printf("face : %sn", a. Ptr->face); printf("suit : %sn", a. suit); printf("suit : %sn", a. Ptr->suit); printf ("Address of a. face : %pn", &a. face); printf ("Address of a. suit : %pn", &a. suit); printf ("Value of a. face : %pn", a. face); printf ("Value of a. suit : %pn", a. suit); Jirasak Sittigorn http: //www. kmitl. ac. th/~ksjirasa/ a 0 0 7 6 0 0 7 A a. suit a. face ตวชกบตวแปรแบบโครงสราง 38 King Mongkut's Institute of Technology

 • เมอสมาชกในโครงสรางเปนตวช (4) 01001012 Principle of Computer Programming a. Ptr struct card {

• เมอสมาชกในโครงสรางเปนตวช (4) 01001012 Principle of Computer Programming a. Ptr struct card { char * face; // 2, 3, . . , 9, Jack, Queen, King, Ace char * suit; // space, heart, diamond, club }; Q h struct card a; u e struct card *a. Ptr; e a a. face = "Queen"; e r a. suit = "heart"; t n a. Ptr = &a; printf("face : %sn", a. face); printf("face : %sn", a. Ptr->face); printf("suit : %sn", a. suit); printf("suit : %sn", a. Ptr->suit); printf ("Address of a. face : %pn", &a. face); printf ("Address of a. suit : %pn", &a. suit); printf ("Value of a. face : %pn", a. face); printf ("Value of a. suit : %pn", a. suit); Jirasak Sittigorn http: //www. kmitl. ac. th/~ksjirasa/ a 0 0 7 6 0 0 7 C a. suit a. face ตวชกบตวแปรแบบโครงสราง 39 King Mongkut's Institute of Technology

ตวอยางการใชตวชกบตวแปรแบบโครงสราง (1) 01001012 Principle of Computer Programming int main(){ struct card { char *

ตวอยางการใชตวชกบตวแปรแบบโครงสราง (1) 01001012 Principle of Computer Programming int main(){ struct card { char * face; char * suit; }; // 2, 3, . . , 9, Jack, Queen, King, Ace // space, heart, diamond, club struct card a; struct card *a. Ptr; a. face = "Ace"; a. suit = "spade"; a. Ptr = &a; printf( "%s %s %sn%s %s %sn", a. face , "of" , a. suit, a. Ptr->face , "of" , a. Ptr->suit, (*a. Ptr). face , "of" , (*a. Ptr). suit ); return 0; a. Ptr } Ace of spade Ace Spade pppp+4 Jirasak Sittigorn http: //www. kmitl. ac. th/~ksjirasa/ a xxxx a. face a. suit pppp+4 xxxx yyyy 40 King Mongkut's Institute of Technology

ตวอยางการใชตวชกบตวแปรแบบโครงสราง (2) 01001012 Principle of Computer Programming int main(){ struct card { char *

ตวอยางการใชตวชกบตวแปรแบบโครงสราง (2) 01001012 Principle of Computer Programming int main(){ struct card { char * face; char * suit; }; // 2, 3, . . , 9, Jack, Queen, King, Ace // space, heart, diamond, club struct card a; struct card *a. Ptr; a. face = "A"; a. suit = "spade"; a. Ptr = &a; printf( "%s %s %sn%s %s %sn", a. face , "of" , a. suit, a. Ptr->face , "of" , a. Ptr->suit, (*a. Ptr). face , "of" , (*a. Ptr). suit ); return 0; } a. Ptr xxxx pppp Ace of spade Jirasak Sittigorn http: //www. kmitl. ac. th/~ksjirasa/ A Spade pppp+2 a a. face a. suit pppp+2 xxxx yyyy 41 King Mongkut's Institute of Technology

โปรแกรมเกบขอมลตวละครจำนวน – ตวอยางการรนโปรแกรม Character 1 name age Character 2 name age Character 3 name

โปรแกรมเกบขอมลตวละครจำนวน – ตวอยางการรนโปรแกรม Character 1 name age Character 2 name age Character 3 name age Character 4 name age Character 5 name age Character 6 name age Jirasak Sittigorn http: //www. kmitl. ac. th/~ksjirasa/ : Gon : 12 : Kuroro : 26 : Hisoka : 27 : Killua : 12 : Machi : 24 : Nobunaga : 30 10 คน (2) 01001012 Principle of Computer Programming Character 7 name : age : Character 8 name : age : Character 9 name : age : Character 10 name : age : Kurapica 17 Ubogin 29 Pakunoda 28 Leorio 19 Gon, 12 Killua, 12 Kurapica, 17 Leorio, 19 43 King Mongkut's Institute of Technology

โปรแกรมเกบขอมลตวละครจำนวน 10 คน | Pointer (3) 01001012 Principle of Computer Programming #include<stdio. h> #define

โปรแกรมเกบขอมลตวละครจำนวน 10 คน | Pointer (3) 01001012 Principle of Computer Programming #include<stdio. h> #define N 10 int main() { int i; struct profile{ char name[30]; int age; }p[N]; struct profile *p. Ptr; p. Ptr = p; //continue Jirasak Sittigorn http: //www. kmitl. ac. th/~ksjirasa/ 44 King Mongkut's Institute of Technology

โปรแกรมเกบขอมลตวละครจำนวน 10 คน | Pointer (4) 01001012 Principle of Computer Programming for(i=0; i<N; i++,

โปรแกรมเกบขอมลตวละครจำนวน 10 คน | Pointer (4) 01001012 Principle of Computer Programming for(i=0; i<N; i++, p. Ptr++) { printf("Character %dn", i+1); printf("tname : "); scanf("%s", p. Ptr->name); printf("tage : "); scanf("%d", &(p. Ptr->age)); } p. Ptr-=N; for(i=0; i<N; i++, p. Ptr++) if(p. Ptr->age < 20) printf("n %s, %d", p. Ptr->name, p. Ptr->age); return 0; } Jirasak Sittigorn http: //www. kmitl. ac. th/~ksjirasa/ 45 King Mongkut's Institute of Technology