Minggu, 06 Desember 2015

Quiz W13 Blog

I want to show about previous W13 Quiz answers.

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! ^.^






    

Rabu, 02 Desember 2015

stdarg.h in C

Hello readers!

i want to show you about stdarg.h in C.

stdarg.h



The stdarg.h header defines a variable type va_list and three macros which can be used to get the arguments in a function when the number of arguments are not known i.e. variable number of arguments.A function of variable arguments is defined with the ellipsis (,...) at the end of the parameter list.

stdarg.h types:
- va_list (type for iterating arguments)

stdarg.h macros:
- va_start (start iterating arguments with a va_list)
- va_arg (retrieve an argument)
- va_end (free a va_list)
- va_copy (copy contents of one va_list to another)

Library macros:
- void va_start(va_list ap, last_arg)
  This macro initializes ap variable to be used with the va_arg and va_end macros. The last_arg is the last known fixed argument being passed to the function i.e. the argument before the ellipsis.

- type va_arg(va_list ap, type)
  This macro retrieves the next argument in the parameter list of the function with type type.

- void va_end(va_list ap)
  This macro allows a function with variable arguments which used the va_start macro to return. If va_end is not called before returning from the function, the result is undefined.

Example:



That's the stdarg.h in C. Hope this will help you. Thanks! :D
nb: Some source that really helps me very much (recommended):
http://www.tutorialspoint.com/c_standard_library/stdarg_h.htm https://en.wikipedia.org/wiki/Stdarg.h