1 / 19

PERTEMUAN IV POINTER

PERTEMUAN IV POINTER. Pointer adalah suatu variabel penunjuk yang menunjuk pada suatu alamat memori komputer

loyal
Download Presentation

PERTEMUAN IV POINTER

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.

E N D

Presentation Transcript


  1. PERTEMUAN IV POINTER Pointer adalahsuatuvariabelpenunjuk yang menunjukpadasuatualamatmemorikomputer Pointer merupakanvariabel level rendah yang dapatdigunakanuntukmenunjuknilai integer, character, float, double, atau single, danbahkantipa-tipe data lain yang didukungolehbahasa C.

  2. Variabelbiasa, sifatnyastatisdansudahpasti, sedangkanpada pointer sifatnyadinamisdandapatlebihfleksibel. • Variabel pointer yang tidakmenunjukpadanilaiapapunberartimemilikinilai NULL, dandisebutsebagaidangling pointer karenanilainyatidakdiinisialisasidantidakdapatdiprediksi.

  3. Operator Alamat • Setiapvariabel yang dideklarasikan, disimpandalamsebuahlokasimemoridanpenggunabiasanyatidakmengetahuidialamatmana data tersebutdisimpan.

  4. #include < stdio.h> #include <conio.h> Int main () { Intnilai 1 = 4; Intnilai 2 = 5; Float nilai 3 = 3.5; Char nama [11] = “abcdefghij “; Int *nilai_p1 = &nilai1; Int *nilai_p2 = &nilai2; Char *nilai_p4 = nama; Float *nilai_p3 = &nilai3; Cout<<“nilai 1 = “<<*nilai_p1<<“, alamat1 = “ <<&nilai_p1; Cout<<“\nnilai 2 = “<<*nilai_p2<<“, alamat2 = “ <<&nilai_p2; Cout<<“\nnilai 3 = “<<*nilai_p3<<“, alamat3 = “ <<&nilai_p3; Cout<<“\nnilai 4 = “<<*nilai_p4<<“, alamat4 = “ <<&nilai_p4; Getch () ; }

  5. 2. Operator Reference (*) Penggunaan operator ini, berartimengaksesnilaisebuahalamat yang ditunjukanolehvariabel pointer. Contoh : Bil1=*Bil2; Dibaca: bil1 samadengannilai yang ditunjukanoleh bil2.

  6. Deklarasivariabel pointer Tipe * nama_pointer; Contoh : #include <iostream.h> Main () { Int ni11 = 5, ni12 = 15; Int *ptr; \Ptr = &ni11; *ptr = 10; Ptr=&ni12; *ptr=20 Cout<<“Nilai 1 = “<<ni11<<“dannilai 2 = “<<ni12; Return

  7. Beberapahaltantang pointer : X = 10; Ptr1 = &X; Ptr2 = ptr1; Ptr1 dan ptr2 menghasilkanalamatvariabel X yang sama.

  8. OPERASI POINTER • Operasipenugasan Nilaidarisuatuvariabel pointer dapatdisalinkevariabel pointer yang lain. Contoh: y = 35; X1 = &y; X2 = x1;

  9. Contoh : #include <conio.h> #include <iostream.h> Int main () { Float nilai, *p1, *p2; Nilai = 14.54; Cout<<“nilai = “<<nilai<<“, alamatnya ‘<<&nilai ; P1 = &nilai; P2 = p1; //operasipemberiannilai, berartialamat x2 samadengan x1 Cout<<“\nnilai p1 = “<<*p2<<“, p1 menunjukanalamat “ <<p1; //padaawalnya p2 masih dangling pointer Cout<<“\nmula-mulainilai p2 = ‘<<*p2 <<“, p2 menunjukanalamat” <<p2; Getch () ; }

  10. 2. Operasiaritmatika • Suatuvariabel pointer hanyadapatdilakukanoperasiaritmatikadengannilai integer saja. • Operasi yang biasadilakukanadalahoperasipenambahandanpengurangan. • Operasipenambahandengansuatunilaimenunjukanlokasi data berikutnya (index selanjutnya) dalammemoti. Begitujugaoperasipengurangan.

  11. #include <iostream.h> #include <conio.h> Void main () { Intnilai [3], *penunjuk; Clrscr () ; Nilai [0] = 125; Nilai [1] =345; Nilai [2] =750; Penunjuk = &nilai [0] ; Cout<<“Nilai “<<*penunjuk <<“ adadialamatmemori “ <<penunjuk; Cout<<“\nNilai “<<*(penunjuk+1) <<“adadialamamemori” <<penunjuk+1; Cout<<“\nNilai “<<* (penunjuk+2) <<“ adadialamatmemori “<<penunjuk+2; Getch () ; }

  12. 3. OperasiLogika #include <iostream.h> #include <conoi.h> Void main () { int *pa, *pb, a = 100, b = 10; Clrscr () ; Pa = &a ; Pb = &b; If (*pa< *pb) { cout<<“pa menunjukankememorilebih RENDAH daripb\n”; } Else if (*pa == *pb ) {cout<<“pa menunjukankememori yang SAMA denganpb\n”; } Else if (*pa > *pb ) {cout<<“pa menunjukankememorilebih TINGGI daripb\n”; } Getch () ; }

  13. ARRAY DAN POINTER // more pointers #include <iostream.h> #include <conio.h> Int main () { Char array [5] ; Char * p ; P = array; *p = ‘a’ ; P++; *p = ‘b’ ; P = &array [2]; *p = ‘c’ ; P = array + 3; *p = ‘d’ ; P =array; * (p+4) = ‘e’ ; For (int n=0; n<5; n++) Cout << array [n] << “, “; Getch () ;

  14. #include <iostream> #include <conio. Const int array = 5; Int main () { Int A [array] ; Const int *pInt = A; For (inti=0; i<array;i++) {cout<<“Input array : “;cin>>A[i] ; } Fof (int n=0; n<array; n++) { cout << “ Element [“ << n << “] = “ pInt++; } Getch () ; }

  15. #include <iostream.h> #include <coin.h> Void main () { Inttgl_ lahir [ ] = { 13,9,1982 }; Int *ptg1; Ptg1 = tgl_lahir; /* ptg1 berisialamat array */ Cout<<“Diaksesdengan pointer \n”; Cout<<“Tanggal = “<< *ptg1; Cout<<“\nBulan = “<< *(ptg1 + 1 ); Cout<<“\nTahun = “<< * (ptg1 + 2); Cout<<“\nDiaksesdengan array biasa\n”; Cout<<“Tanggal = “<< tgl_lahir [0] ; Cout<<“\nBulan = “<<tg1_1ahir [1] ; Cout<<“\nTahun = “<< tg1_1ahir [2] ; Getch () ; }

  16. Latihanbuatinputanscrdinamisdari program dibawahini : // more pointers #include <iostream.h> #include <conio.h> Int main () { Char array[5] ; Char * p; P = array; *p = ‘a’; P++; *p = ‘b’ ; P = *array [2]; *p = ‘c’; P = array + 3; *p = ‘d’; P = array; * (p+4) = ‘e’ ; For (int n=0; n<5; n++) Cout << array [n] << “, “; Getch () ;

  17. Jawab : // more pointers #include <iostream.h> #inclide <conio.h> Int main () { Char * p; P = array; For (inti=0;i<=4;i++) { cout<<“\nInput array [‘<<i<<“]=“;cin>>*p; p++; } For (int n=0; n<5; n++) Cout << array [n] << “, “; Getch () ; }

  18. TUGAS SILAHKAN DICOBA PROGRAM YANG ADA HINGGA BISA DIJALANKAN (RUN), KEMUDIAN BUATKAN ALUR LOGIKANYA.Catatan : Tidakada yang copy-paste punyakawan !Dikumpul : 2 Minggudarisekarang.

  19. SampaiJumpa…….

More Related