1. When is the right time to use:
a. struct
b. pointer/reference
c. function
d. array
2. Write an example that uses combination all of items above, for example:
void bla( Cat * x ) { // x is pointer to struct Cat
x -> .. // this how to access data member using pointer to struct
}
ANSWERS
1. a. We use struct when we want to work with data that has some items. for example, there is an ID Card that contains data like a name, address, sex, and age. But it is different with array, because array is only can has some elements with the same type. (In Indonesia: Ketika kita ingin bekerja dengan data yang mempunyai sejumlah item. contohnya, sebuah kartu identitas yang mengandung data seperti nama, alamat, jenis kelamin, dan usia. tetapi struct berbeda dengan array, karena array hanya dapat mengandung beberapa elemen dengan tipe sama.).
b. We use pointer/reference when we want to make an object data that contains an address that point the location of memory where a score is stored. (In Indonesia: Ketika kita ingin membuat sebuah objek data berisi alamat yang menunjukkan lokasi memori dimana suatu nilai tersimpan.).
c. We use function when we want to make codes that is for do a task and that codes will be executed if it's names is called. (In Indonesia: Ketika kita ingin membuat sekumpulan kode yang ditujukan untuk melaksanakan suatu tugas tertentu dan kode-kode terbsebut akan dijalankan bila namanya dipanggil.).
d. We use array when we want to make a data with a scores that has same type in certain order that uses the same name. (In Indonesia: Ketika kita ingin membuat sebuah data dengan nilai-nilai bertipe sama dalam urutan tertentu yang menggunakan sebuah nama yang sama.).
2. Here is an example about pointer that can be used to point the array
Here is the explanation about the example:
After ptr is set to point the array score via:
Setelah ptr diatur agar menunjuk ke array score melalui:
ptr = score;
Then accessing array data can be do via pointer ptr. As always, *ptr will be declare the first element in array score. So, if there is a statement like this:
Maka pengaksesan data array dapat dilakukan melalui pointer ptr. Seperti biasa, *ptr akan menyatakan isi elemen pertama di array nilai. Jadi, jika kemudian terdapat pernyataan seperti berikut:
ptr++;
ptr will be point to the next element in array ptr. Absolutely, to show all the element of array score via ptr, we will do via:
ptr akan menunjuk ke elemen berikutnya di array ptr. Tentu saja, untuk menampilkan semua elemen array nilai melalui ptr, perlu dilakukan melalui:
for(x=0;x<sizeof(score)/sizeof(int);x++)
In this case,
Dalam hal ini,
sizeof(score)/sizeof(int)
declares the code for get the amount of array element.
menyatakan kode untuk mendapatkan jumlah elemen array.
THANK YOU! ^.^
Tidak ada komentar:
Posting Komentar