ตัวชี้หรือพอยน์เตอร์ (pointer)

Download ตัวชี้หรือพอยน์เตอร์ (Pointer)

Post on 05-Jan-2016

55 views

Category:

Documents

0 download

Embed Size (px)

DESCRIPTION

(Pointer). (Pointer). - PowerPoint PPT Presentation

TRANSCRIPT

  • created by Dararat Saelee , Computer Sciences Department, Faculty of Science, PSU-HatYai (Pointer)

    created by Dararat Saelee , Computer Sciences Department, Faculty of Science, PSU-HatYai

  • created by Dararat Saelee , Computer Sciences Department, Faculty of Science, PSU-HatYai* (Pointer)

    created by Dararat Saelee , Computer Sciences Department, Faculty of Science, PSU-HatYai

  • created by Dararat Saelee , Computer Sciences Department, Faculty of Science, PSU-HatYai*Pointer () (address)

    created by Dararat Saelee , Computer Sciences Department, Faculty of Science, PSU-HatYai

  • created by Dararat Saelee , Computer Sciences Department, Faculty of Science, PSU-HatYai*Pointer () score 120 int score; score = 120;

    120score

    created by Dararat Saelee , Computer Sciences Department, Faculty of Science, PSU-HatYai

  • created by Dararat Saelee , Computer Sciences Department, Faculty of Science, PSU-HatYai*Pointer () ptr 120 int *ptr; *ptr = 120;

    ptr 120

    created by Dararat Saelee , Computer Sciences Department, Faculty of Science, PSU-HatYai

  • created by Dararat Saelee , Computer Sciences Department, Faculty of Science, PSU-HatYai* type *variable_name type = int, float * = variable_name = Example int *ptr; char *pt; float *ft;

    created by Dararat Saelee , Computer Sciences Department, Faculty of Science, PSU-HatYai

  • created by Dararat Saelee , Computer Sciences Department, Faculty of Science, PSU-HatYai* ()

    Example int *ptr;

    char *pt;

    float *ft;

    created by Dararat Saelee , Computer Sciences Department, Faculty of Science, PSU-HatYai

  • created by Dararat Saelee , Computer Sciences Department, Faculty of Science, PSU-HatYai*- & (ampersand) (Address) - *

    created by Dararat Saelee , Computer Sciences Department, Faculty of Science, PSU-HatYai

  • created by Dararat Saelee , Computer Sciences Department, Faculty of Science, PSU-HatYai* () score 120 int score; score = 120; printf(&score = %d,&score);

    120&score = 888

    created by Dararat Saelee , Computer Sciences Department, Faculty of Science, PSU-HatYai

  • created by Dararat Saelee , Computer Sciences Department, Faculty of Science, PSU-HatYai* ()Example int *ptr;

    ptr 111 *ptr 52

    Example char *pt;

    pt 200 *pt G

    created by Dararat Saelee , Computer Sciences Department, Faculty of Science, PSU-HatYai

  • created by Dararat Saelee , Computer Sciences Department, Faculty of Science, PSU-HatYai*int *pt pt integer&aaddress operator apt = &a; pt address operator a*pt pt b = *pt b pt

    created by Dararat Saelee , Computer Sciences Department, Faculty of Science, PSU-HatYai

  • created by Dararat Saelee , Computer Sciences Department, Faculty of Science, PSU-HatYai* ()Exampleint a, *b;a = 10;b = &a; printf (a=%d, *b); a b100100 0370100100

    created by Dararat Saelee , Computer Sciences Department, Faculty of Science, PSU-HatYai

  • created by Dararat Saelee , Computer Sciences Department, Faculty of Science, PSU-HatYai* ()Exampleint a, *b;*b = 10;a = *b; printf (a=%d, a); a b

    0100 03701010*bDemo Program

    created by Dararat Saelee , Computer Sciences Department, Faculty of Science, PSU-HatYai

  • created by Dararat Saelee , Computer Sciences Department, Faculty of Science, PSU-HatYai* 1. + 2. - 3. + + 14. - - 1

    created by Dararat Saelee , Computer Sciences Department, Faculty of Science, PSU-HatYai

  • created by Dararat Saelee , Computer Sciences Department, Faculty of Science, PSU-HatYai*Example int *p; pointer integer p++; p--; 4 byte address p = p+2; p 8 (4x2) byte address p = p-5; p 20 (4x5) byte address

    Demo Program

    created by Dararat Saelee , Computer Sciences Department, Faculty of Science, PSU-HatYai

  • created by Dararat Saelee , Computer Sciences Department, Faculty of Science, PSU-HatYai*Example int *p; pointer integer x = *p++; or x = *(p++); x = p p 4 y = (*p)++; p 1

    created by Dararat Saelee , Computer Sciences Department, Faculty of Science, PSU-HatYai

  • created by Dararat Saelee , Computer Sciences Department, Faculty of Science, PSU-HatYai*(passing by address)type function-name (formal parameters)formal parameters : pointer variablesEx. int func1 (int *a, int *b) { return *a + *b; }

    created by Dararat Saelee , Computer Sciences Department, Faculty of Science, PSU-HatYai

  • created by Dararat Saelee , Computer Sciences Department, Faculty of Science, PSU-HatYai*(passing by address)var = function-name (arguments)arguments : address of variablesEx. z = func1 (&x, &y);

    address x y

    created by Dararat Saelee , Computer Sciences Department, Faculty of Science, PSU-HatYai

  • created by Dararat Saelee , Computer Sciences Department, Faculty of Science, PSU-HatYai*(passing by address) int func1 (int *a, int *b) { return *a + *b; }

    void main() { int x,y,z; z = func1 (&x, &y); }x

    y5

    7name value address00140

    0014200140 , 00142*(00140)+*(00142)5+7 = 1212

    created by Dararat Saelee , Computer Sciences Department, Faculty of Science, PSU-HatYai

  • created by Dararat Saelee , Computer Sciences Department, Faculty of Science, PSU-HatYai*Demo program

    created by Dararat Saelee , Computer Sciences Department, Faculty of Science, PSU-HatYai

    1

    #include

    void swap (int * , int *);

    void main()

    {

    int a , b ;

    printf (enter number1 and number2 : );

    scanf (%d%d, &a , &b);

    swap (&a , &b);

    printf (number1 : %d and number2 : %d\n, a, b);

    }

    void swap (int *x , int *y)

    {

    int temp;

    temp = *x;

    *x = *y;

    *y = temp;

    }

  • created by Dararat Saelee , Computer Sciences Department, Faculty of Science, PSU-HatYai* 1

    created by Dararat Saelee , Computer Sciences Department, Faculty of Science, PSU-HatYai

  • created by Dararat Saelee , Computer Sciences Department, Faculty of Science, PSU-HatYai*Class Exercise. 1 2

    created by Dararat Saelee , Computer Sciences Department, Faculty of Science, PSU-HatYai

  • created by Dararat Saelee , Computer Sciences Department, Faculty of Science, PSU-HatYai*

    created by Dararat Saelee , Computer Sciences Department, Faculty of Science, PSU-HatYai

  • created by Dararat Saelee , Computer Sciences Department, Faculty of Science, PSU-HatYai* ()Exampleint x[4] = {1,2,3,4};int *px[4];px[0] = &x[0];px[1] = &x[1];px[2] = &x[2];px[3] = &x[3];

    created by Dararat Saelee , Computer Sciences Department, Faculty of Science, PSU-HatYai

  • created by Dararat Saelee , Computer Sciences Department, Faculty of Science, PSU-HatYai* () int x[4] = {1, 2, 3, 4}; int *px[4];x[0] x[1] x[2] x[3]px[0] px[1] px[2] px[3]0210 0212 0214 0216 03EE 03F0 03F2 03F4 1 2 3 4px[0] = &x[0];px[1] = &x[1];px[2] = &x[2];px[3] = &x[3];0210 0212 0214 0216

    created by Dararat Saelee , Computer Sciences Department, Faculty of Science, PSU-HatYai

  • created by Dararat Saelee , Computer Sciences Department, Faculty of Science, PSU-HatYai* () int x[4] = {1, 2, 3, 4}; int *px[4];x[0] x[1] x[2] x[3]px[0] px[1] px[2] px[3]0210 0212 0214 0216 03EE 03F0 03F2 03F4 1 2 3 4*px[0] = x[0];*px[1] = x[1];*px[2] = x[2];*px[3] = x[3];0210 0212 0214 0216

    created by Dararat Saelee , Computer Sciences Department, Faculty of Science, PSU-HatYai

  • created by Dararat Saelee , Computer Sciences Department, Faculty of Science, PSU-HatYai*Demo program

    created by Dararat Saelee , Computer Sciences Department, Faculty of Science, PSU-HatYai

    2

    #include

    int array[10] = {1, 3, 4, 5, 7, 9, 0, 8, 1, 6};

    int sum (int *data , int size);

    void main()

    {

    printf (sum of data in array = %d\n, sum (array , 10));

    }

    int sum (int *data , int size)

    {

    int sumall = 0;

    int j;

    for (j = 0; j < size; j++)