session 08

Upload: chaubinhkhang110686

Post on 14-Apr-2018

223 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/30/2019 Session 08

    1/30

    Con tr

    Bi 8

  • 7/30/2019 Session 08

    2/30

    Elementary Programming with C/Session 8/ Slide 2 of 30

    Mc tiu bi hc

    Tm hiu v con tr v khi no th s dngcon tr

    Cch s dng bin con tr v cc ton t con

    trGn gi tr cho con trPhp ton trn con tr

    So snh con trCon tr v mng mt chiuCon tr v mng nhiu chiu

    Tm hiu cch cp pht b nh

  • 7/30/2019 Session 08

    3/30

    Elementary Programming with C/Session 8/ Slide 3 of 30

    Con tr l g?

    Con tr l mt bin, n cha a ch nh camt bin khc

    Nu mt bin cha a ch ca mt bin khc,

    th bin ny c gi l con tr tr nbin thhai

    Con tr cung cp phng thc truy xut gin

    tip n gi tr ca mt phn t d liuCc con tr c th tr n cc bin c kiu d

    liu c bn nh int, char,double, hay d liu tphp nh mnghoc cu trc.

  • 7/30/2019 Session 08

    4/30

    Elementary Programming with C/Session 8/ Slide 4 of 30

    Con tr c s dng lm g?

    Cc tnh hung con tr c th c s dng: tr v nhiu hn mt gi tr t mt hm

    truyn mng v chui t mt hm nmt hm khc thun tin hn lm vic vi cc phn t ca mng thay

    v truy xut trc tip vo cc phn t ny cp pht b nh v truy xut b nh

    (Cp pht b nh trc tip)

  • 7/30/2019 Session 08

    5/30

    Elementary Programming with C/Session 8/ Slide 5 of 30

    Bin con tr

    Khai bo con tr: ch ra mt kiu c s vmt tn bin c t trc bi du *

    C php khai bo tng qut:

    V d:

    type *name;

    int *var2;

  • 7/30/2019 Session 08

    6/30

    Elementary Programming with C/Session 8/ Slide 6 of 30

    Cc ton t con tr

    Hai ton t c bit c s dng vi con tr:

    & l ton t mt ngi v n tr v a ch nhca ton hng

    Ton t *l phn b xung ca ton t &. y l

    ton t mt ngi v n tr v gi tr cha trongvng nh c tr n bi bin con tr

    v& *

    var2 = &var1;

    temp = *var2;

  • 7/30/2019 Session 08

    7/30Elementary Programming with C/Session 8/ Slide 7 of 30

    Gn tr i vi con tr

    Cc gi tr c th c gn cho con trthng qua ton t &.

    ptr_var = &var;

    y a ch ca var c lu vo binptr_var.

    Cng c th gn gi tr cho con tr thngqua mt bin con tr khc tr c cngkiu. ptr_var = &var;

    ptr_var2 = ptr_var;

  • 7/30/2019 Session 08

    8/30Elementary Programming with C/Session 8/ Slide 8 of 30

    C th gn gi tr cho cc bin thngqua con tr

    *ptr_var = 10;

    Cu lnh trn gn gi tr 10 cho binvar nu ptr_var ang tr n var

    Gntr i vi con tr (tt)

  • 7/30/2019 Session 08

    9/30Elementary Programming with C/Session 8/ Slide 9 of 30

    Php ton con tr Ch c th thc hin php ton cng v tr trn

    con trint var, * ptr_var;

    ptr_var = & var;var = 500;

    ptr_var ++;

    Gi s bin varc lu tr ti a ch 1000 ptr_var lu gi tr 1000. V s nguyn c kch

    thc l 2 bytes, nn sau biu thcptr_var++; ptr_var s c gi tr l 1002 m

    khng l 1001

  • 7/30/2019 Session 08

    10/30Elementary Programming with C/Session 8/ Slide 10 of 30

    Php ton con tr (tt)

  • 7/30/2019 Session 08

    11/30Elementary Programming with C/Session 8/ Slide 11 of 30

    Mi ln con tr c tng tr, n tr n nh caphn t k tip

    Mi ln con tr c gim tr, n tr n nhca phn t ng trc n

    Tt c con tr s tng hoc gim tr theo kchthc ca kiu d liu m chng ang tr n

    Php ton con tr (tt)

  • 7/30/2019 Session 08

    12/30Elementary Programming with C/Session 8/ Slide 12 of 30

    So snh con tr

    Hai con tr c th c so snh trong mt biuthc quan h nu chng tr n cc bin c cngkiu d liu

    Gi s ptr_a v ptr_b l hai bin con tr tr n ccphn t d liu a v b. Trong trng hp ny, ccphp so snh sau l c th:

  • 7/30/2019 Session 08

    13/30Elementary Programming with C/Session 8/ Slide 13 of 30

    So snh con tr (tt)

  • 7/30/2019 Session 08

    14/30Elementary Programming with C/Session 8/ Slide 14 of 30

    Con tr v mng mt chiu

    a ch ca mt phn t mng c th cbiu din theo hai cch:

    S dng k hiu & trc mt phn tmng. S dng mt biu thc trong ch s

    ca phn t c cng vo tn ca

    mng.

  • 7/30/2019 Session 08

    15/30Elementary Programming with C/Session 8/ Slide 15 of 30

    #include

    void main(){

    static int ary[10]

    ={1,2,3,4,5,6,7,8,9,10};

    int i;for (i= 0;i

  • 7/30/2019 Session 08

    16/30Elementary Programming with C/Session 8/ Slide 16 of 30

    Con tr v mng mt chiu-v d tt

  • 7/30/2019 Session 08

    17/30Elementary Programming with C/Session 8/ Slide 17 of 30

    Mng hai chiu c th c nh ngha nh lmt con tr tr ti mt nhm cc mng mtchiu lin tip nhau

    Khai bo mt mng hai chiu c th nh sau:

    thay v

    data_type (*ptr_var) [expr 2];

    data_type (*ptr_var) [expr1] [expr 2];

    Con tr v mng a chiu

  • 7/30/2019 Session 08

    18/30Elementary Programming with C/Session 8/ Slide 18 of 30

    Con tr v chui#include #include

    void main (){

    char a, str[81], *ptr;

    printf(\nEnter a sentence:);

    gets(str);printf(\nEnter character to search for:);

    a = getche();

    ptr = strchr(str,a);

    /* return pointer to char*/

    printf( \nString starts at address: %u,str);printf(\nFirst occurrence of the character is

    at address: %u ,ptr);

    printf(\n Position of first occurrence(starting

    from 0)is: % d, ptr_str);

    }

  • 7/30/2019 Session 08

    19/30Elementary Programming with C/Session 8/ Slide 19 of 30

    Con tr v chui (tt)

  • 7/30/2019 Session 08

    20/30Elementary Programming with C/Session 8/ Slide 20 of 30

    Cp pht b nh

    Hm malloc() l mt trong cc hm c s

    dng thng xuyn nht thc hin vic cp

    phtbnht vng nhcn t do.

    Tham sca hm malloc() l mts nguyn xc

    nhs bytes cncp pht.

  • 7/30/2019 Session 08

    21/30Elementary Programming with C/Session 8/ Slide 21 of 30

    Cp pht b nh (tt)

  • 7/30/2019 Session 08

    22/30Elementary Programming with C/Session 8/ Slide 22 of 30

    Hm free()

    Hm free() c s dng gii phng bnh khi n khng cn dng na.C php:

    void free(void*ptr);Hm ny gii phng khng gian c tr biptr, dng cho tng lai.

    ptrphi c dng trc vi li gi hmmalloc(), calloc(), hoc realloc().

  • 7/30/2019 Session 08

    23/30

    Elementary Programming with C/Session 8/ Slide 23 of 30

    #include #include /*required for the malloc and free functions*/

    int main(){

    int number;

    int *ptr;int i;

    printf("How many ints would you like store? ");

    scanf("%d", &number);

    ptr = (int *) malloc (number*sizeof(int));

    /*allocate memory */

    if(ptr!=NULL) {

    for(i=0 ; i

  • 7/30/2019 Session 08

    24/30

    Elementary Programming with C/Session 8/ Slide 24 of 30

    for(i=number ; i>0 ; i--) {

    printf("%d\n",*(ptr+(i-1)));

    /* print out in reverse order */

    }

    free(ptr); /* free allocated memory */return 0;

    }

    else {

    printf("\nMemory allocation failed -

    not enough memory.\n");

    return 1;

    }

    }

    Hm free() - tt

  • 7/30/2019 Session 08

    25/30

    Elementary Programming with C/Session 8/ Slide 25 of 30

    Hm calloc()

    calloctng t nh malloc, nhng im khc bitchnh l mc nhin gi tr 0 c lu vo khng gian

    b nh va cp pht

    callocyu cu hai tham s Tham s th nht l s lng cc bin cn cp pht

    b nh Tham s th hai l kch thc ca mi binC php:

    void *calloc( size_t num, size_t size );

  • 7/30/2019 Session 08

    26/30

    Elementary Programming with C/Session 8/ Slide 26 of 30

    #include #include

    int main() {

    float *calloc1, *calloc2;

    int i;

    calloc1 = (float *) calloc(3,

    sizeof(float));

    calloc2 = (float *)calloc(3, sizeof(float));

    if(calloc1!=NULL && calloc2!=NULL){

    for(i=0 ; i

  • 7/30/2019 Session 08

    27/30

    Elementary Programming with C/Session 8/ Slide 27 of 30

    free(calloc1);

    free(calloc2);

    return 0;

    }else{

    printf("Not enough memory\n");

    return 1;

    }}

    Hm calloc() - tt

  • 7/30/2019 Session 08

    28/30

    Elementary Programming with C/Session 8/ Slide 28 of 30

    Hm realloc()

    C th cp pht li cho mt vng c cp (thm/bts bytes) bng cch s dng hm realloc, m khng lmmt d liu.

    reallocnhn hai tham s Tham s th nht l con tr tham chiu n b nh

    Tham s th hai l tng s byte mun cp pht C php:void *realloc( void *ptr, size_t size );

  • 7/30/2019 Session 08

    29/30

    Elementary Programming with C/Session 8/ Slide 29 of 30

    #include

    #include

    int main(){

    int *ptr;

    int i;ptr = (int *)calloc(5, sizeof(int *));

    if(ptr!=NULL) {

    *ptr = 1; *(ptr+1) = 2;

    ptr[2] = 4; ptr[3] = 8; ptr[4] = 16;

    ptr = (int *)realloc(ptr, 7*sizeof(int));if(ptr!=NULL){

    printf("Now allocating more memory...\n");

    ptr[5] = 32; /* now it's legal! */

    ptr[6] = 64;

    Hm realloc() - tt

  • 7/30/2019 Session 08

    30/30

    for(i=0;i