An Image/Link below is provided (as is) to download presentation
Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.Content is provided to you AS IS for your information and personal use only.Download presentation by click this link.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.During download, if you can't get a presentation, the file might be deleted by the publisher.
12 12 #include<stdio.h> void main() { int num = 15; printf(“Number is %d\n”,num); printf(“Address of num is %u\n”,&num); }
13 13 #include<stdio.h> void main() { int num = 15; printf(“Number is %d\n”,num); printf(“Address of num is %u\n”,&num); printf(“Value of address %u is %d\n”,&num,*(&num)); }
14 14 #include<stdio.h> void main() { int num = 15; int *address; address = # printf(“Number is %d\n”,num); printf(“Address of num is %u\n”,address); printf(“Value of address %u is %d\n”,address, *address); }
15 15 #include<stdio.h> void main() { int num = 15; int *address; address = # printf(“Number is %d\n”,num); printf(“Address of num is %u\n”,address); printf(“Value of address %u is %d\n”,address, *address); *address = 100; printf(“Number is %d\n”,num); printf(“Value of address %u is %d\n”,address, *address); }
20 24 24 #include<stdio.h> void main() { int *pt,a,b; a = 20; pt = &a; b = *pt; printf(“%d\n”,b); }
25 25 #include<stdio.h> void main() { int *pt,a; a = 20; pt = &a; printf(“Address pointed by pt store value of %d\n ”,*pt); *pt = 10; printf(“After indirect assignment a = %d\n”,a); }
6 ? 2 ? ? ? a a b b c c ? ? ? ? ? ? p p q q r r 26 26 #include <stdio.h> void main() { int a,b,c; int *p,*q,*r; a = 6; b = 2; p = &b; q = p; r = &c; p = &a; *q = 8; *r = *p; *r = a + *q + *&c; printf(“%d %d %d\n”,a,b,c); printf(“%d %d %d”,*p,*q,*r); } ตัวอย่างที่ 3
6 6 8 2 ? ? a a b b c c ? ? ? ? ? ? p p q q r r 27 27 #include <stdio.h> void main() { int a,b,c; int *p,*q,*r; a = 6; b = 2; p = &b; q = p; r = &c; p = &a; *q = 8; *r = *p; *r = a + *q + *&c; printf(“%d %d %d\n”,a,b,c); printf(“%d %d %d”,*p,*q,*r); } ตัวอย่างที่ 3
6 8 6 a b c p q r 28 28 #include <stdio.h> void main() { int a,b,c; int *p,*q,*r; a = 6; b = 2; p = &b; q = p; r = &c; p = &a; *q = 8; *r = *p; *r = a + *q + *&c; printf(“%d %d %d\n”,a,b,c); printf(“%d %d %d”,*p,*q,*r); } 6 8 20 a b c p q r ตัวอย่างที่ 3
6 8 20 a b c p q r 29 29 #include <stdio.h> void main() { int a,b,c; int *p,*q,*r; a = 6; b = 2; p = &b; q = p; r = &c; p = &a; *q = 8; *r = *p; *r = a + *q + *&c; printf(“%d %d %d\n”,a,b,c); printf(“%d %d %d”,*p,*q,*r); } 6 8 20 6 8 20 ตัวอย่างที่ 3
30 30 #include <stdio.h> void main() { int a = 15; int b,c; int *pa, *pb, *pc; pa = &a; a = *pa; b = *pa + 5; pb = &b; pc = &c; *pc = a + b; printf(“Data in a,b,c = %d %d %d\n”,a,b,c); printf(“Address of a,b,c = %u %u %u\n”,pa,pb,pc); }
การส่งค่ากลับของ Function 34 34 • ปกติการส่งค่ากลับของ Function จะมีเพียง 1 ค่าเท่านั้น โดยใช้คำสั่ง Return • แต่ในกรณีที่ต้องการให้มีการส่งค่ากลับหลายค่า จะต้องใช้ตัวแปร Pointer ช่วย • การ Pass ค่าไปยัง Function • Pass by Value (การส่งค่าของข้อมูลไปยัง Function) • Pass by Reference (การส่งตำแหน่งของข้อมูลไปยัง Function)
35 35 Pass by Value 10 15 q p #include <stdio.h> void pass(int ,int); void main() { int p = 10, q = 15; printf(“Before : p = %d, q = %d\n”,p, q); pass(p, q); printf(“After : p = %d, q = %d\n”,p, q); } void pass(int p, int q) { p = 5; q = 20; printf(“In function : p = %d, q = %d\n”,p, q); } copy ค่า q 10 15 p 10 15 q p q 5 20 p
Pass by Reference 36 36 10 15 q p #include <stdio.h> void pass(int *,int *); void main() { int p = 10, q = 15; printf(“Before : p = %d, q = %d\n”,p, q); pass(&p, &q); printf(“After : p = %d, q = %d\n”,p, q); } void pass(int *p, int *q) { ++*p; *q = 20; printf(“In function : p = %d, q = %d\n”,*p, *q); } ชี้ไปยัง q p 11 20 q p q p ตัวอย่างที่ 1
Pass by Reference 37 37 50 100 q p #include <stdio.h> void swap(int *,int *); void main() { int p = 50, q = 100; printf(“Before : p = %d, q = %d\n”,p, q); swap(&p, &q); printf(“After : p = %d, q = %d\n”,p, q); } void swap(int *pp, int *qq) { int tmp; tmp = *pp; *pp = *qq; *qq = tmp; pp = &q; printf(“In function : p = %d, q = %d\n”,*pp, *qq); } ชี้ไปยัง qq pp 100 50 q p qq pp 50 tmp ตัวอย่างที่ 2
38 38 Pass by Reference 3 10 5 a b c ชี้ไปยัง #include <stdio.h> void calcu(int *, int *, int *); void main() { int a = 3, b = 10, c= 5; calcu(&a, &b, &c); printf(“A = %d,B = %d,C = %d\n”,a,b,c); } void calcu(int *pa, int *pb, int *pc) { *pa = *pa + 5; *pb = *pb + 10; *pc = * pc + * pb; } pb pa pc 8 20 25 a b c ชี้ไปยัง pb pc pa ตัวอย่างที่ 3
49 49 #include <stdio.h> int array[]={4,5,8,9,8,1,0,1,9,3}; int *array_ptr; void main() { array_ptr = array; while(*array_ptr !=0) ++ array_ptr; printf(“Number of elements before zero %d\n”, array_ptr-array); } ตัวอย่าง 6