1 / 21

POINTER

POINTER. Outline. Pointer dan Struktur Pointer dan Array Pointer dan Function. Pointer dan Struktur (Step-By-Step). 1. Seperti diketahui, deklarasi struktur sbb : struct tag { char Fname[20]; char Lname[20]; int age; };. Pointer dan Struktur.

dianne
Télécharger la présentation

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. POINTER

  2. Outline • Pointer dan Struktur • Pointer dan Array • Pointer dan Function

  3. Pointer danStruktur(Step-By-Step) 1. Seperti diketahui, deklarasi struktur sbb : struct tag { char Fname[20]; char Lname[20]; int age; };

  4. Pointer dan Struktur 2. Deklrasikansebuah variable pointer struct tag *st_ptr; 3. Deklarasikan variable struktur struct tag my_struct; 4. Mengarahkan pointer kepada variable struktur st_ptr = &my_struct; 5. Mengakses / memberinilaimelalui de-referensi pointer. Model Integer (*st_ptr).age = 63; atau st_ptr->age = 63; atau Model Char strcpy(st_ptr->nama,"Agus");

  5. LatihanPointer danStruktur • Buatlahstrukturttgmhs (nama, usia, alamat) • Berikannilaiterhadapmhstsb (inisialiasi/input) • Tampilkanhasilisiannilaitsb

  6. Kebimbangan : • Cara mengaksesatribut salah : scanf(“%d”, usia); apasalahnya ? usiaituadadidalamnyastruktur, sehinggaharusdiawalidengannamastrukturnya scanf(“%d”, st_ptr->usia);

  7. Kebimbangan : 2. Cara memasukkanNilai (*st_ptr).usia = 63; Atau scanf(“%d”, st_ptr->usia); Solusinya ? Pilihsalahsatu

  8. Pointer dan Array 1. Seperti diketahui, deklarasi pointer sbb : int *ptr; 2. Mendeklarasikan array : int my_array[] = {1,23,17,4,-5,100};

  9. Pointer dan Array 3. Mengarahkan pointer ke array elemenpertama ptr = &my_array[0]; Atau ptr = my_array; Tapitidakbolehdibaliksepertiini : my_array = ptr;

  10. ` 4. Menampilkan Nilai Array, ada 2 cara pilihan : 1.Melalui : variable array cout<<my_array[1]; Atau 2. Melalui : variable pointer (recommended) cout<< *(ptr+1);

  11. Pertanyaan Piyecaranedatane array kuwi, diketokkenanglayar monitor, umpamanetampilanekoyongisoriki : Array ke 0 = 1 Array ke 1 = 23 Array ke 2 = 17 Array ke 3 = 4 Array ke 4 = -5 Array ke 5 = 100

  12. Pointer dan Array #include <iostream.h> intmy_array[] = {12,23,17,43,-5,10}; //Deklarasi Array int *ptr; //Deklarasipointer int main(void) { inti; ptr = &my_array[0]; // mengarahkan pointer ke array ke 0 for (i = 0; i < 6; i++) { cout<<" ptr + "<< i<<" = "<< *(ptr+i)<<"\n"; } }

  13. Kegalauan • Menampilkansemuaelemen array. SALAH : perintahnyasatupersatu cout<<*(ptr+0); cout<<*(ptr+1); cout<<*(ptr+2); cout<<*(ptr+3); BENAR : gunakanperulangan (for / while) for (i = 0; i < 4; i++) { cout<<*(ptr+i); }

  14. Next Weekmaterial Pointer and Function Prepare, please!

  15. Pointer dan Function 1. Variable pointer dapat didefinisikan (deklarasikan) sebagai lokal maupun global 2. Juga variable pointer dapat didefinisikan (deklarasikan) sebagai parameter didalam sebuah function

  16. Letak Deklarasi • Global Diletakkan diatas program utama dan diluar Function. • Lokal Diletakkan didalam program utama atau didalam Function. • Parameter Diletakkan didalam nama function

  17. Contoh Deklarasi Global Variable my_struct dan *st_ptr

  18. Contoh Deklarasi Lokal Variable my_struct dan *st_ptr

  19. Contoh Deklarasi Parameter Variable pointer *p bertipe struct tag

  20. void show_name(struct tag *p, int x, int y) { } Si pemanggil function : Show_name (st_ptr, 9,4);

More Related