1 / 25

PEMROGRAMAN SISTEM POINTER Pertemuan X

PEMROGRAMAN SISTEM POINTER Pertemuan X. ALAMAT & POINTER. Pointer sesungguhnya berisi alamat memory yang menyimpan data. A. 13. 12. 12. A. 13. 13. Variabel Biasa. Pointer. Mengetahui alamat variabel. Dengan menambah operator & didepan nama variabel Contoh : Int A = 5;

kiley
Download Presentation

PEMROGRAMAN SISTEM POINTER Pertemuan X

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. PEMROGRAMAN SISTEMPOINTERPertemuan X

  2. ALAMAT & POINTER • Pointer sesungguhnya berisi alamat memory yang menyimpan data. A 13 12 12 A 13 13 Variabel Biasa Pointer

  3. Mengetahui alamat variabel • Dengan menambah operator & didepan nama variabel • Contoh : Int A = 5; cout<<“Variabel A = << &A;

  4. Void main() { int a = 5; int b = 10; Cout<<“ isi variabel A = “<< a<<endl; Cout<<“ isi variabel B = “<< b<<endl; Cout<<“ alamat variabel A = “<< &a<<endl; Cout<<“ alamat variabel B = “<< &b<<endl; }

  5. Variabel pointer Tipe *nama_variabel • Contoh : Int *a; Float *b; Char *c;

  6. Mengisi variabel • Contoh : Int a = 55; Int *b; • Untuk mengisi : B = &a;

  7. void main() { int a = 5; int b = 10; int *c; int *d; c = &a; d = &b; cout << " alamat a = " << &a << endl; cout << " c = " << c << endl; cout << " alamat b = " << &b << endl; cout << " d = " << d << endl; getch(); }

  8. Mengakses nilai yang ditunjuk void main() { int a = 5; int b = 10; int *c; int *d; c = &a; d = &b; cout << " a = " << a << endl; cout << " c = " << *c << endl; cout << " b = " << b << endl; cout << " d = " << *d << endl; getch(); }

  9. void main() { int a = 5; int b = 10; int *c; int *d; c = &a; d = &b; cout << " a = " << a << endl; cout << " c = " << *c << endl; cout << " b = " << b << endl; cout << " d = " << *d << endl; cout << endl; *c = 15; *d = 20; cout << " a = " << a << endl; cout << " c = " << *c << endl; cout << " b = " << b << endl; cout << " d = " << *d << endl; getch(); }

  10. Pointer Void • Untuk memebuat pointer yang tidak bertipe yakni dengan meletakkan kata kunci pada void pada bagian penentu tipe pointer. • Contoh : void *ptr; • Merupakan pernyataan untuk mendefinisikan ptr sebagai variabel pointer void. • Suatu pointer void adalah pointer yang dapat menunjuk ke sembarang tipe data. • Misalnya, Anda dapat mengatur agar pointer ini menunjuk ke tipe data int, tetapi di saat lain diperlukan untuk menunjuk data bertipe float

  11. void main() { void *ptr; int vint = 50; float vfl = 51.5; ptr = &vint; cout << "Nilai yang ditunjuk oleh ptr = " << *(int *)ptr << endl; ptr = &vfl; cout << "Nilai yang ditunjuk oleh ptr : " <<*(float *) ptr << endl; getch(); }

  12. void main() { void *ptr; int vint = 50; ptr = &vint; cout << "Nilai vint = " << vint << endl; *(int *)ptr = 77; cout << "Nilai vint = " << vint << endl; getch(); }

  13. Pointer & Array Int tgl [] = {4, 6, 2008}; Int *ptgl; • Untuk menunjuk : Ptgl = tgl; • Bedanya : C = &A Ptgl = &tgl[0];

  14. void main() { void *ptr; int vint = 50; ptr = &vint; cout << "Nilai vint = " << vint << endl; *(int *)ptr = 77; cout << "Nilai vint = " << vint << endl; getch(); }

  15. void main() { int tgl[]= {4, 6, 2008}; int *ptgl; ptgl = tgl; for (int i = 0; i < 3 ; i++) cout << *( ptgl + i ) << endl; getch(); }

  16. void main() { int tgl[]= {4, 6, 2008}; int *ptgl; ptgl = tgl; for (int i = 0; i < 3 ; i++) { cout << *ptgl << endl; cout << ptgl << endl; ptgl++; } getch(); }

  17. Pointer String void main() { char *p = " BAMBANG "; cout << p << endl; getch(); }

  18. void main() { char n[] = "BAMBANG "; char *p = "BAMBANG "; cout << n << endl; cout << p << endl; p++ ; cout << n << endl; cout << p << endl; getch(); }

  19. Array Pointer void main() { char *n[] = {"BAMBANG", "RUDI", "SANTI"}; for(int i = 0; i<3; i++) cout << n[i] << endl; getch(); }

  20. Pointer Menunjuk Pointer Pointer 2 Pointer 1 Variabel X

  21. Contoh : Int x; Int *p1; Int **p2; • Perintah yang diberikan : P1 = &x; P2 = &p1

  22. void main() { int x = 55; int *p1; int **p2; p1 = &x; p2 = &p1; cout << “ Variabel X = “ << x << endl; cout << “ Variabel p1 = “ << *p1 << endl; cout << “ Variabel p2 = “ << **p2 << endl; getch(); }

  23. Pointer sebagai argumen fungsi Void ubah(int &x); void main() { int x = 10; cout << “ Variabel X = “ << x << endl; ubah(x); cout << “ Variabel X = “ << x << endl; getch(); } Void ubah(int &x) { x = 20; }

  24. void ubah(int *x); void main() { int x = 10; cout << “ Variabel X = “ << x << endl; ubah(&x); cout << “ Variabel X = “ << x << endl; getch(); } void ubah(int *x) { *x = 20; }

  25. Pointer sebagai keluaran fungsi char *bulan(int n); void main () { int bln; cout << “ Bulan = “; cin >> bln; cout << bulan(bln) << endl; } char *bulan(int n) { static char *bulan[] = { “salah”, “Januari”,”Februari”,”Maret”,”April”,”Mei”,“Juni”, ”Juli”,”Agustus”,”September”,”Oktober”,”Nopember”,”Desember” }; return ((n < 1 || N > 12 ) ? bulan[0] : bulan [n] ); }

More Related