Download
pertemuan 2 data komposit structure n.
Skip this Video
Loading SlideShow in 5 Seconds..
Pertemuan 2 Data Komposit Structure PowerPoint Presentation
Download Presentation
Pertemuan 2 Data Komposit Structure

Pertemuan 2 Data Komposit Structure

151 Views Download Presentation
Download Presentation

Pertemuan 2 Data Komposit Structure

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

  1. Pertemuan 2Data Komposit Structure Matakuliah : T0026/Struktur Data Tahun : 2005 Versi : 1/1

  2. Learning Outcomes Pada akhir pertemuan ini, diharapkan mahasiswa akan mampu : • Mahasiswa dapat menghasilkan program modular yang menggunakan structure of structure.

  3. Outline Materi • Deklarasi Structure • Akses data elemen struct • array of struct • Structure of structure • struct sebagai parameter fungsi • contoh program dengan struct

  4. Definisi Structure • Elemen array : heterogen • Elemen struct disebut field • Akses elemen array : random melalui nama_struct.nama_field • Penempatan field dalam memory : berurutan, secara logikal dan fisikal • Jumlah dan tipe field menentukan jumlah memori

  5. FIELD FILE/TABLE Matakuliah KodeMTK NamaMTK SKS T0026 Struktur Data 6 STRUCTURE/RECORD A0102 Pancasila 2 ME351 Kalkulus 4 Contoh Structure Hubungan antara file-structure-field:

  6. Pendeklarasian Structure Syntax Pada C : struct <struct_name>{ <type> <elemen_name1>; <type> <elemen_name2>; - } <structure_variable>; Contoh (1) : struct Matakuliah{ char KodeMTK[6]; char NamaMTK[40] int SKS; }; Variabel X dg tipe struct Matakuliah didefinisikan : struct Matakuliah X; Contoh (2) : struct Matakuliah{ char KodeMTK[6]; char NamaMTK[40] int SKS; } X, Y; Contoh (3): struct { char KodeMTK[6]; char NamaMTK[40] int SKS; } X;

  7. Deklarasi struct struct person { char name[10]; int age; }; Address person.name = a Address person.age = a + 10 Jumlah memory = sizeof (name) + sizeof (int) = 12

  8. Addressing pada Struct • Address suatu field = base address + ofset (field) • Ofset (field) : jarak antara base address ke awal field

  9. Operasi pada Struct Nama struct • Create • Retrive • Store struct person { char name[10]; int age; }; person p1, *p2; x = p1.age ; y = p2->age; p1.name = “Andi”; p2->name = “Ani”; Nama variabel

  10. Akses Struct complex c1; complex *pc; pc = (complex*) malloc(sizeof(complex)); atau pc = &c1; printf (“…”, &c1.re, c1.re, &c1.im, c1.im); printf (“…”, &pc->re, pc->re, &pc->im, pc->im);

  11. Passing Parameter(1) struct complex{ float re; float im; }; complex add (complex cc1, complex cc2) { complex z; z.re = cc1.re + cc2.re; z.im = cc1.im + cc2.im; return (z); }

  12. Passing Parameter(2) void init (complex *px, float r, float i){ px->re = r; px->im = i; } void main() { complex c1, c2, c, *pc; init (&c1, 5., 7.); init (&c2, 10., 13.); c = add (c1, c2);

  13. Array of struct (1) struct person{ char name[10]; int age; address addr; }; person p[80], *pp; Array p adalah array 1 dimensi. Sizeof(person) = 12 Address p[50].name dan p[50].age ? • struct address{ • char street[10]; • int number • };

  14. Cara mengakses field dgn pointer Array of struct (2) pp = &p; //pointer to struct hrs dialokasikan (*pp).name pp->name pp++ //geser ke structure berikutnya

  15. Struct of struct struct person{ char name[10]; int age; address addr; }; person p[80], *pp; struct address{ char street[10]; int number }; Cara akses field struct of struct : p[i].addr.number pp->addr.number