1 Pertemuan 2 Data 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 Hubungan antara file-structure-field : Matakuliah 4KalkulusME351 2PancasilaA0102 6Struktur DataT0026 SKSNamaMTKKodeMTK FIELD STRUCTURE/RECORD FILE/TABLE Contoh Structure
6 Syntax Pada C : struct { ; - } ; Contoh (1) : struct Matakuliah{char KodeMTK[6]; char NamaMTK[40] intSKS; }; Variabel X dg tipe struct Matakuliah didefinisikan : struct Matakuliah X; Contoh (2) : struct Matakuliah{char KodeMTK[6]; char NamaMTK[40] intSKS; } X, Y; Contoh (3): struct {char KodeMTK[6]; char NamaMTK[40] intSKS; } X; Pendeklarasian Structure
7 struct person { char name[10]; intage; }; Address person.name = Address person.age = + 10 Jumlah memory = sizeof (name) + sizeof (int) = 12 Deklarasi struct
8 Addressing pada Struct Address suatu field = base address + ofset (field) Ofset (field) : jarak antara base address ke awal field
9 Operasi pada Struct Create Retrive Store struct person { char name[10]; intage; }; personp1, *p2; x = p1.age; y = p2->age; p1.name = “Andi”; p2->name = “Ani”; Nama struct 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 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); } Passing Parameter(1)
12 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); Passing Parameter(2)
13 Array of struct (1) struct person{ charname[10]; intage; addressaddr; }; person p[80], *pp; Array p adalah array 1 dimensi. Sizeof(person) = 12 Address p[50].name dan p[50].age ? struct address{ charstreet[10]; int number };
14 pp = &p; // pointer to struct hrs dialokasikan (*pp).name pp->name pp++ // geser ke structure berikutnya Cara mengakses field dgn pointer Array of struct (2)
15 Struct of struct struct person{ charname[10]; intage; addressaddr; }; person p[80], *pp; struct address{ charstreet[10]; int number }; Cara akses field struct of struct : p[i].addr.number pp->addr.number