พอยน์เตอร์ ( pointer)

Download พอยน์เตอร์ ( Pointer)

Post on 25-Jan-2016

79 views

Category:

Documents

4 download

Embed Size (px)

DESCRIPTION

( Pointer). CSC-113 Structure Programming. . Pointer Pointer Function Pointer Array Pointer String Array Pointer Pointer Pointer. 2. 2. Pointer . - PowerPoint PPT Presentation

TRANSCRIPT

  • CSC-113 Structure Programming (Pointer)

  • ** Pointer Pointer Function Pointer Array Pointer String Array Pointer Pointer Pointer

  • *Pointer * Data type (Address) Ptr PointerN integer1001350 1001memory100110021003PtrN 1003

    1001

    350..

  • *Pointer * 2 1. Direct Pointer Variable 2. Indirect Pointer Variable

  • *1. Direct Pointer Variable* Ptr PointerN integer1001350 1001memory100110021003PtrN 1003mem. address

    1001

    350..

  • *2. Indirect Pointer Variable* Pointer Pointer Pointer to Pointer

  • 250 3000**Ptr1 PointerN integer5000memory1000...3000Ptr2N 10005000...Ptr1Ptr2 Pointer3000 5000mem. address12

    3000

    250

    5000..

  • * Pointer* Array String Function Function Array Data Structure Link lists, Binary Tree 1

  • ** Pointer Pointer Function Pointer Array Pointer String Array Pointer Pointer Pointer

  • type *prt_name; type *prt1_name, *prt2_name, ;

    Pointertype Pointer ptr_name Pointer * Pointerint *p, *a;

  • * (Asterisk) & (Ampersand) char m=T;char *pm;pm = &m;

  • #includevoid main(){int num = 15;printf(Number is %d\n,num); printf(Address of num is %u\n,&num);}

  • #includevoid 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));}

  • #includevoid 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);}

  • #includevoid 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);}

  • #include void main() { int *p,q; q = 100; p = &q; printf(%d,*p); } p Pointer q integer? 112341? pq 5000 () 3000 ()

  • q 100 12? pq 5000 () 3000 ()#include void main() { int *p,q; q = 100; p = &q; printf(%d,*p); }1234

  • address q p ( p q) 100 135000 pq 5000 () 3000 ()#include void main() { int *p,q; q = 100; p = &q; printf(%d,*p); }1234

  • pointer p 100 145000 pq 5000 () 3000 ()#include void main() { int *p,q; q = 100; p = &q; printf(%d,*p); }123412

  • p Pointer q integer ? 2123

    41? pq 5000 () 3000 ()#include void main() { int *p,q; p = &q; *p = 1000; //q= 1000; printf(%d,q); }

  • 2123

    4#include void main() { int *p,q; p = &q; *p = 1000; //q= 1000; printf(%d,q); } address q p ( p q) ? 25000 pq 5000 () 3000 ()

  • 1000 address p 1000 235000 pq 5000 () 3000 ()123

    4#include void main() { int *p,q; p = &q; *p = 1000; //q= 1000; printf(%d,q); }1000

  • q 1000 245000 pq 5000 () 3000 ()123

    4#include void main() { int *p,q; p = &q; *p = 1000; //q= 1000; printf(%d,q); }

  • #includevoid main(){int *pt,a,b;a = 20;pt = &a; b = *pt;printf(%d\n,b);}

  • #includevoid 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);}

  • #include 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

  • #include 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

  • #include 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); } 36820abcpqr

  • #include 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); } 36 8 206 8 20

  • #include 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);}

  • 1. MySalary = 5000 // int2. PMySalary int3. PMySalary MySalary4. Memory MySalary5. Memory PMySalary; 6. MySalary7. PMySalary 8. &(*PMySalary) ?

  • #include void main() { int MySalary = 5000; //1 int *PMySalary; //2 PMySalary = &MySalary; //3 printf("%d\n",&MySalary); //4 printf("%d\n",PMySalary); //5 printf("%d\n",MySalary); //6 printf("%d\n",*PMySalary); //7 printf("%d\n",&(*PMySalary)); //8 }

  • ** Pointer Pointer Function Pointer Array Pointer String Array Pointer Pointer Pointer

  • Function Function 1 Return Pointer Pass Function Pass by Value ( Function) Pass by Reference ( Function)

  • Pass by Value#include 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); }1015pq1015pqcopy 520pq1015pq

  • Pass by Reference#include 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); } pq 1015pq pq1120pq 1

  • Pass by Reference#include 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); } ppqq50100pq pqppqq50tmp10050 2

  • Pass by Reference#include 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;}3a 310b5c pa pb pc8a20b25c pa pb pc

  • ** Pointer Pointer Function Pointer Array Pointer String Array Pointer Pointer Pointer

  • Pointer Array Pointer int number[] = {10,20,30,40,50}; Address Array : number + j &number[j]= number[j] Array j : number[j] *(number + j)

  • Address

    1020304050

    FFECFFEEFFE0FFF2FFF4

    number[0] *(number+0)number[1] *(number+1)number[2] *(number+2)number[3] *(number+3)number[4] *(number+4)

    &number[0] number+0&number[1] number+1&number[2] number+2&number[3] number+3&number[4] number+4

  • Pointer Arraychar str[80], *pl; pl = str; pl = &str[0]; str[4] *(pl + 4)strpl

  • #include void printarray(int *); void main() { int array[ ] = {100, 200, 300, 400, 500}; printarray(array);}void printarray(int *ptr) { for (int i=0; i < 5; i++) printf(Array#%d = %d\n,i, ptr[i]); } 1 : Array#0 = 100Array#1 = 200Array#2 = 300Array#3 = 400Array#4 = 500

  • #include void printarray(int *); void main() { int array[ ] = {100, 200, 300, 400, 500}; printarray(array);}void printarray(int *ptr) { for (int i=0; i < 5; i++) printf(Array#%d = %d\n,i, *(ptr+i)); } 1 : Array#0 = 100Array#1 = 200Array#2 = 300Array#3 = 400Array#4 = 500

  • #define SIZE 5#include void inc_array(int *, int, int); void main() { int number[] = {100, 200, 300, 400, 500}; inc_array(number, SIZE, 5); //for (int i =0; i
  • #include void main() { int a[5] = {0,1,2,3,4}; int *ptr;ptr = a;printf(%d\n,a[0]);printf(%d\n,*ptr);printf(%d\n,*(ptr+1));printf(%d\n,*(a+2));printf(%d\n,ptr[3]);} 3

  • #include void main() {int number[3];int *pt;number[0] = 100;number[1] = 200;number[2] = 300;pt = number;printf(number[0] = %d\n,*pt);printf(number[1] = %d\n,*(pt+1));printf(number[2] = %d\n,*(pt+2));} 4

  • #include void main() {int number[3];int *pt,i;number[0] = 200;number[1] = 400;number[2] = 600;pt = number;for( i = 0;i
  • #include 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

  • #include #include void main() {char sentence[10];char *Ptr;int count;Ptr = sentence;for(count = 0; count
  • #include long SumArray(int *tbl, int ct);void main() {int Y[5]={5,7,9,11,13};long s;s = SumArray(Y,5);printf("Sum is %d", s);}long SumArray(int *tbl, int ct){int i;long sum = 0;for (i=0; i
  • ** Pointer Pointer Function Pointer Array Pointer String Array Pointer Pointer Pointer

  • Pointer String char *ptr = HELLO; char array[6] = HELLOptr1500Address

    HELL0\0

    150015011502150315041506

  • #include void main() { char *s; printf(Enter String : ); gets(s);// s = dddd; printf(s); // printf(%