Variable number of function args in C
Hi!
Does any of you know how to make a fuction in C that accepts variable number of argumets? I googled a bit but haven't found anything interesting.
Or do I have solve this using overriding the function, like
void func(arg1);
void func(arg1, arg2);
etc.?
Thanks.
Google for va_start, va_arg, and va_end.
Briefly, you define the function like so: void func(arg1, ...). Note the ..., that means you can supply any number of arguments after arg1. Inside the function you use the va_ functions to access the variable arguments.
Briefly, you define the function like so: void func(arg1, ...). Note the ..., that means you can supply any number of arguments after arg1. Inside the function you use the va_ functions to access the variable arguments.
Ok, I found some documetation using linux man and found this piece of code:
But I don't have a clue if I use the function correctly. Is there sth wrong? Because this only prints out "char" and that's all.
#include <stdio.h>#include <stdarg.h> void foo(char *fmt, ...) { va_list ap; int d; char c, *s; va_start(ap, fmt); while (*fmt) switch(*fmt++) { case 's': /* string */ s = va_arg(ap, char *); printf("string %s\n", s); break; case 'd': /* int */ d = va_arg(ap, int); printf("int %d\n", d); break; case 'c': /* char */ /* need a cast here since va_arg only takes fully promoted types */ c = (char) va_arg(ap, int); printf("char %c\n", c); break; } va_end(ap); }int main() { foo("ce", "sls", "d32"); return 0;}
But I don't have a clue if I use the function correctly. Is there sth wrong? Because this only prints out "char" and that's all.
It looks all right to me, but I just glanced over it. I just wanted to point out that C doesn't support overriding functions.
At a glance, I'm thinking that what the function is expecting is something like:
foo("csd", 'e', "sls", 32);
The first parameter specifies two things: 1) how many further parameters there are (based on the number of characters), and 2) what the type of each of those parameters are. So "csd" is saying that there are three further parameters, the first is a character, the second is a string (character pointer), and the third is an integer.
Oh yeah. Now I get it. Thanks a lot guys, you were a great help!
Edit: One more thing: C doesn't support fucntion overriding???
Edit: One more thing: C doesn't support fucntion overriding???
here you go...
#include <stdio.h>#include <stdarg.h>void output(const char *fmt, ...){ // buffer, going to be used much like sprintf was char buffer[80]; // arg list va_list vl; // start the list va_start(vl,fmt); // format all the stuff and put in in the buffer vsprintf(buffer, fmt,vl); // end the list va_end(vl); // yay! print our formatted stuff printf("%s\n", buffer);}int main(void){ int cards = 81; // useless code just made to show off the power of our new function! for (int i = 10; i > 0; i--) output("There are %d Cards in the %s! And %d divided by %f is%f", cards, "deck", cards, 2.3, cards--/2.3); return 0;}
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement