How to pass arguments to a function using a loop
I need to pass a large number of arguments to a function which takes variable number of arguments, such as gtk_list_store_new. But it doesn't look nice if i write something like gtk_list_store_new(NUM,TYPE_A,TYPE_B,TYPE_C,...,TYPE_OMEGA); because of large number of arguments. And, it will be a trouble to change number of columns because of need to manually change arguments to large number of such functions.
So, how can i pass all the arguments to a function using a loop? Something like Code:
for(i=0;i<NUM;i++) P.S. i mean C language. |
You can look into variable-length argument lists (example here), which is similar to how "printf", "scanf", etc, work. I've never implemented one of these functions, but a quick read on a different site says that there is no information about the type and number of subsequent arguments, so from that to me it seems you can pass your different types TYPE_A, TYPE_B, etc.
Alternatively, you can wrap the arguments in a structure (excuse any syntax errors) Code:
struct arg_type Code:
// map argument types to specific constant indices |
Thanks for the reply. I found using your link that there's no portable way of passing variable list of arguments built on run time to functions such as f(a,...).
Anyway, i found that there does exist a function which will accept an array instead of explicit arguments - gtk_list_store_newv. |
All times are GMT -5. The time now is 09:48 PM. |