pointer vn

Download Pointer vn

Post on 13-Jan-2017

37 views

Category:

Technology

0 download

Embed Size (px)

TRANSCRIPT

  • C:7 CON TRKhi nimnh nghaCc thao tcMt s php tonCon tr v mngTruyn a ch cho tham s bi con trCon tr tr n con trCon tr khng kiuCon tr v cu trcB nh ng

  • 1. Khi nimChng ta bit cc bin u c kch thc, kiu d liu xc nh (bin tnh) chnh l cc nh m chng ta c th truy xut di cc tn. Cc bin ny c lu tr ti nhng ch c th trong b nh, tn ti trong sut thi gian thc thi chng trnhHn ch ca bin tnh:Cp pht nh d: Gy lnh phCp pht nh thiu: Li chng trnh

  • 1. Khi nimC++ cung cp mt kiu bin gi l con tr vi nhng c im sau:Ch pht sinh trong qu trnh thc hin chng trnhKhi chy chng trnh, kch thc ca bin, vng nh v a ch vng nh c cp pht cho bin c th thay iSau khi thc hin xong c th gii phng tit kim ch trong b nhKch thc ca bin con tr khng ph thuc kiu d liu, n lun c kch thc c nh l 2 byte (ty thuc HH)

  • 2. nh nghaCon tr n gin ch l a ch ca mt v tr b nh v cung cp cch gin tip truy xut d liu trong b nh

    Chng 5

    type *pointer_name;Kiu d liu c tr tiKhng phi l kiu ca con tr

    Tn con tr ngha: Khai bo mt bin c tn l pointer_name dng cha a ch ca cc bin c kiu l type

  • 2. nh nghaGi tr ca mt bin con tr l a ch m n tr nNu cha mun khai bo kiu d liu m con tr ch n, ta c th khai bo nh sau:

    void *;

  • V dint *number;char *character;float *greatnumber;y l ba khai bo ca con trMi bin tr ti mt kiu d liu khc nhau Chng u chim mt lng b nh nh nhau (kch thc ca mt bin con tr ty thuc vo h iu hnh) D liu m chng tr ti khng chim lng b nh nh nhau, mt kiu int, mt kiu char v ci cn li kiu float

  • 3. Cc thao tc trn con trLy a chTham chiu

  • Ton t ly a ch (&)Khai bo mt bin th n phi c lu tr trong mt v tr c th trong b nhChng ta khng quyt nh ni no bin c t Vy:Ai t v tr ca bin?Chng ta c th bit bin c lu tr u ?

    H iu hnhiu ny c th c thc hin bng cch t trc tn bin mt du v (&), c ngha l "a ch ca".

  • Ly a ch (tt)Gi s rng bin andy c t nh c a ch 1776 v chng ta vit nh sau: int andy, fred; int* ted;andy = 25;fred = andy;ted = &andy;

  • Ton t ly a ch (&)C php:

    =&;

    ngha: Gn a ch ca cho con tr

  • Ton t tham chiu (*)C th truy xut trc tip n gi tr c lu tr trong bin c tr bi con tr bng cch t trc tn bin con tr mt du sao (*) - y c dch l "gi tr c tr bi"

    V vy, nu chng ta vit: int beth = *ted;C++ th c n l: "beth bng gi tr c tr bi ted" beth s mang gi tr 25, v ted bng 1776 v gi tr tr bi 1776 l 25

    *;

  • V dBn phi phn bit c rng ted c a ch 1776, nhng *ted (vi mt du sao ng trc) tr ti gi tr c lu tr trong a ch 1776, l 25

    beth = ted; // beth c gn bng ted ( 1776 )beth = *ted; // beth c gn bng gi tr c tr bi ted ( 25 )

    int andy, fred, *ted;andy = 25;fred = andy;ted = &andy;beth=*ted;

  • Kt lun v & v *Ton t ly a ch (&)N c dng nh l mt tin t ca bin v c th c dch l "a ch ca

    &variable1 c th c c l "a ch ca variable1

    Ton t tham chiu (*)N ch ra rng ci cn c tnh ton l ni dung c tr bi biu thc c coi nh l mt a ch. c dch l "gi tr c tr bi"..

    *mypointer c c l "gi tr c tr bi mypointer"

  • 3. Mt s php tonPhp gn con trPhp cng, tr con tr vi 1 s nguynPhp tr 2 con trTng (gim) 1 ngi trn bin con tr

  • Php gn con tr2 con tr cng kiu c th gn cho nhau:

    int a, *p, *b ;float *f;a = 5 ;p = &a ;b = p ; /* ng */f = p ; /* sai do khc kiu */

  • Php gn con trTa cng c th p kiu con tr theo c php:

    (*);

    int a, *p, *q ;float *f;a = 5 ;p = &a ;q = p ; /* ng */f = (float*)p; /* ng, nh p kiu*/

  • V sao?#include int main (){ int value1 = 5, value2 = 15; int *mypointer; mypointer = &value1; *mypointer = 10; mypointer = &value2; *mypointer = 20; cout
  • M tvalue1value211121114

    mypointerGn cho mypointer a ch ca value1 [dng ton t ly a ch (&)]Gi tr c tr bi mypointergn 10 cho gi tr c tr bi mypointer10a ch binGi tr ca bin

  • #include int main (){ int value1 = 5, value2 = 15; int *p1, *p2; p1 = &value1; // p1 = a ch ca value1 p2 = &value2; // p2 = a ch ca value2 *p1 = 10; // gi tr tr bi p1 = 10 *p2 = *p1; // gi tr tr bi p2 = gi tr tr bi p1 p1 = p2; // p1 = p2 (php gn con tr) *p1 = 20; // gi tr tr bi p1 = 20 cout

  • Cng, tr con tr vi 1 s nguynTa c th cng (+), tr (-) 1 con tr vi 1 s nguyn dng N; kt qu tr v l mt con tr, con tr ny ch n vng nh cch vng nh hin ti N

    int *a;a = (int*) malloc(20); /* cp vng nh 20byte=10 s nguyn*/int *b, *c;b = a + 7;c = b - 3;

  • Tr 2 con trTr 2 con tr cng kiu s cho 1 s nguyn (int). y l khong cch (phn t) gia 2 con tr

    V d:

    c-a=4

    Khng th cng 2 con tr vi nhaub = a + 7;c = b - 3;

  • Tng (gim) 1 ngi trn bin con trGi s chng ta c 3 con tr sau:

    char *mychar;short *myshort;long *mylong; Chng ln lt tr ti nh 1000, 2000 v 3000. Nu chng ta vit

    mychar++;myshort++;mylong++; 100120013001100120033004Nguyn nhn:Khi cng thm 1 vo mt con tr th n s tr ti phn t tip theo c cng kiu m n c nh ngha, v vy kch thc tnh bng byte ca kiu d liu n tr ti s c cng thm vo bin con tr

  • Tng (gim) 1 ngi trn bin con trVit cch khc:mychar = mychar + 1;myshort = myshort + 1;mylong = mylong + 1;

  • Lu th t u tin ca ton tCn phi cnh bo bn rng c hai ton t tng (++) v gim (--) u c quyn u tin ln hn ton t tham chiu (*), v vy biu thc sau y c th dn ti kt qu sai: *p++;*p++ = *q++;Lnh u tin tng ng vi *(p++) iu m n thc hin l tng p (a ch nh m n tr ti ch khng phi l gi tr tr ti). Lnh th hai, c hai ton t tng (++) u c thc hin sau khi gi tr ca *q c gn cho *p v sau c q v p u tng ln 1. Lnh ny tng ng vi:*p = *q;p++;q++;

  • V d:#include #include void main(){const int SIZE=5;int i, *point, a[SIZE]={98,87,76,65,54};clrscr();point=&a[0]; // point=gradefor (i=0;i
  • 5. Con tr v mngTrong thc t, tn ca mt mng tng ng vi a ch phn t u tin ca n, ging nh mt con tr tng ng vi a ch ca phn t u tin m n tr ti, v vy thc t chng hon ton nh nhau. V d, cho hai khai bo sau: int numbers[20];int *p; lnh sau s hp l: p = numbers;

  • 5. Con tr v mng y p v numbers l tng ng v chng c cng thuc tnh, s khc bit duy nht l chng ta c th gn mt gi tr khc cho con tr p trong khi numbers lun tr n phn t u tin trong s 20 phn t kiu int m n c nh ngha. V vy, khng ging nh p - l mt bin con tr bnh thng, numbers l mt con tr hng. Lnh gn sau y l khng hp l:

    numbers = p;

    Bi v numbers l mt mng (con tr hng) v khng c gi tr no c th c gn cho cc hng

  • Con tr - Mng#include int main (){ int numbers[5]; int *p; p = numbers; *p = 10; p++; *p = 20; p = &numbers[2]; *p = 30; p = numbers + 3; *p = 40; p = numbers; *(p+4) = 50; for (int n=0; n
  • Truy cp cc phn t mng theo dng con trC php:

    &[0] t/ng vi

    & [] t/ng vi +

    [] t/ng vi *( + )

  • #include #include /* Nhap mang binh thuong*/void NhapMang(int a[ ], int N){int i;for(i=0;i

  • Truy cp n tng phn t ang c qun l bi con tr theo dng mngC php:

    [] t/ng vi *( + )

    &[] t/ng vi ( + )Tn bin con trS nguyn

  • C mng a gm cc phn t kiu s nguyn, khi vit: a[3] ; //truy xut n phn t th 4 trong mng a. Nu s dng ch s th my tnh s da vo ch s ny v a ch bt u ca vng nh dnh cho mng xc nh a ch ca phn t m ta mun truy xut ti truy xut phn t a[3], my tnh xc nh nh sau:&a[3]= a[0] + (3*2) // gi s kiu int chim 2 byte b nh

  • V d:#include #include void main(){const int SIZE=5;int i, *addr, a[SIZE]={98,87,76,65,54};clrscr();addr=&a[0];for (i=0;i
  • Xem xt

  • 6. Truyn a ch cho tham s bi con tr#include #include void swap(int*, int*);void main(){int i=5,j=10;clrscr();cout
  • V d#include int addition (int a, int b){ return (a+b); }int subtraction (int a, int b){ return (a-b); }int (*minus) (int,int) = subtraction;int operation (int x, int y, int (*functocall) (int,int)){ int g; g = (*functocall) (x,y); return (g);}int main (){ int m,n; m = operation (7, 5, &addition); n = operation (20, m, minus); cout
  • 7. Con tr tr n con trchar a;char * b;char ** c;a = 'z';b = &a;c = &b;gi s rng a,b,c c lu cc nh 7230, 8092 and 10502, ta c th m t on m trn nh sauc l mt bin c kiu (char **) mang gi tr 8092*c l mt bin c kiu (char*) mang gi tr 7230**c l mt bin c kiu (char) mang gi tr 'z'

  • 8. Con tr khng kiu Con tr khng kiu l mt loi con tr c bit. N c th tr ti bt k loi d liu no, t gi tr nguyn hoc thc cho ti mt xu k t. Hn ch duy nht ca n l d liu c tr ti khng th c tham chiu ti mt cch trc tip (chng ta khng th dng ton t tham chiu * vi chng) v di ca n l khng xc nhV vy chng ta phi dng n ton t chuyn kiu d liu hay php gn chuyn con tr khng kiu thnh mt con tr tr ti mt loi d liu c th

  • V d#include void increase (void* data, int type){ switch (type) { case sizeof(char) : (*((char*)data))++; break; case sizeof(short): (*((short*)data))++; break; case sizeof(long) : (*((long*)data))++; break; }}int main (){ char a = 5; short b = 9; long c = 12; increase (&a,sizeof(a)); increase (&b,sizeof(b)); increase (&c,sizeof(c)); cout
  • 9. Con tr v cu trcNh bt k cc kiu d liu no khc, cc cu trc c th c tr n bi con tr. Quy tc hon ton ging nh i vi bt k kiu d liu c bn no:

    struct movies_t { char title [50]; int year;}; movies_t amovie; movies_t *pmovie; y amovie l mt i tng c kiu movies_t v pmovie l mt con tr tr ti i tng movies_t.

  • V d

  • on m trn gii thiu mt iu quan trng: ton t ->. y l mt ton t tham chiu ch dng tr ti cc cu trc v cc lp (class). N cho php chng ta khng phi dng ngoc mi khi tham chiu n mt phn t ca cu trc. Trong v d ny chng ta s dng: movies->title n c th c dch thnh: (*movies).titlec hai biu thc movies->title v (*movies).title u hp l v chng u dng tham chiu n phn t title ca cu trc c tr bi movies. Bn cn phn bit r rng vi: *movies.title n tng ng vi *(movies.title)lnh ny dng tnh ton gi tr c tr bi phn t title ca cu trc movies, trong trn