Materi 4 - Struct Atau Record Revisi

  • Uploaded by: Euis Marlina
  • 0
  • 0
  • November 2019
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Materi 4 - Struct Atau Record Revisi as PDF for free.

More details

  • Words: 747
  • Pages: 17
Struct atau Record

Euis Marlina, S.Kom Email : [email protected] http://euismarlina.edublogs.org HP : 08179424319

Mata Kuliah Struktur Data - 2008

Pengantar 





Struct terdapat pada bahasa C/C++, sedangkan Record pada bahasa Pascal. Structure is a collection of variables under a single name. Variables can be of any type: int, float, char etc. Struct/Record adalah sekumpulan elemen data yang memiliki tipe dan panjang data yang berbeda-beda tapi memiliki nama yang sama. Mata Kuliah Struktur Data - 2008





Setiap elemen data disebut dengan member. The main difference between structure and array is that arrays are collections of the same data type and structure is a collection of variables under a single name.

Mata Kuliah Struktur Data - 2008





Pertama yang harus anda ketahui adalah bahwa struct itu akan membentuk tipe data baru. Satu kali struct didefinisikan, maka tipe data baru berupa nama struct tersebut dapat digunakan dalam program sebagaimana tipe data lainnya seperti int, char, short, dll. Mata Kuliah Struktur Data - 2008

Deklarasi Struct 

The structure is declared by using the keyword struct followed by structure name, also called a tag. Then the structure members (variables) are defined with their type and variable names inside the open and close braces { and }. Finally, the closed braces end with a semicolon denoted as ; Mata Kuliah Struktur Data - 2008

Syntax untuk membentuk struct: struct nama_struct { tipe_data_member1 nama_member1; tipe_data_member2 nama_member2; tipe_data_member3 nama_member3; }nama_objek; Nama_objek merupakan sebuah pengenal bagi objek yang menggunakan tipe struct tersebut. Mata Kuliah Struktur Data - 2008

Contoh : Struct customer struct product { { Int custNum; Int salary; int weight; Float commision; float price; }; } ; product apple, banana, melon; In the above example, it is seen that variables of different types such as int and float are grouped in a single structure name product and customer. Objek apple, banana dan melon menggunakan tipe data product. Mata Kuliah Struktur Data - 2008

Kita dapat mendeklarasikan objek yang akan menggunakan struct tersebut pada saat deklarasi struct. Seperti di bawah ini

struct product { int weight; float price; }apple, banana, melon; Mata Kuliah Struktur Data - 2008

Mengakses Member Untuk mengakses member struct pada masing2 objek, gunakan tanda titik(.) yang disisipkan antara nama objek dan nama member struct. Contoh : apple.weight apple.price banana.weight banana.price melon.weight melon.price 

Mata Kuliah Struktur Data - 2008

Pengalokasian Memori What happens when this is defined?  When structure is defined, it allocates or reserves space in memory. The memory space allocated will be cumulative of all defined structure members.  In the above example, there are 3 structure members: custnum, salary and commission. Of these, two are of type in and one is of type float. If integer space allocated by a system is 2 bytes and float four bytes the above would allo9acter 2bytes for custnum, 2 bytes for salary and 4 bytes for commission. Mata Kuliah Struktur Data - 2008

Contoh Program 1 #include #include #include #include

<stdlib.h> <string.h>

struct movies_t { char title[50]; int year; } mine, yours;

Mata Kuliah Struktur Data - 2008

void printmovie (movies_t movie); int main () { char mystr[5]; strcpy(mine.title,"2001 A Space Odyssey“); mine.year = 1968; cout << "Enter title: "; cin.getline(yours.title,sizeof(yours.title)); cout << "Enter year: "; cin.getline(mystr, sizeof(mystr)); yours.year = atoi(mystr); Mata Kuliah Struktur Data - 2008

cout << "My favorite movie is:\n"; printmovie (mine); cout << "And yours is:\n "; printmovie (yours); getch(); return 0; } void printmovie(movies_t movie) { cout << movie.title; cout << " (" << movie.year << ")\n"; } Mata Kuliah Struktur Data - 2008

strcpy() 



Pada contoh program di atas ada fungsi strcpy() dan atoi() Strcry() adalah fungsi yang ada pada file header string.h yang berfungsi untuk mengcopy string. syntax : char * strcpy ( char * destination, const char * source );

Mata Kuliah Struktur Data - 2008

Contoh Program 2 #include #include <string.h> #include int main () { char str1[]="Sample string"; char str2[40]; char str3[40]; strcpy(str2,str1); strcpy(str3,"copy successful"); cout<<"str1: ”<<str1<<endl; cout<<“str2: “<<str2<<endl; cout<<“str3: “<<str3<<endl; getch(); return 0; } Mata Kuliah Struktur Data - 2008

atoi() 

atoi() adalah fungsi yang ada pada file header stdlib.h yang berfungsi untuk menkonversi string menjadi integer. syntax : int atoi ( const char * str );

Fungsi lain: atol / strtol -> Convert string to long integer atof -> Convert string to double Mata Kuliah Struktur Data - 2008

Referensi  

http://www.cplusplus.com/reference/ Exforsys Inc® Execution for system

Mata Kuliah Struktur Data - 2008

Related Documents

Struct
June 2020 12
Struct
April 2020 11
Struct
June 2020 12
Struct
November 2019 22

More Documents from ""