Jump to content
  • Advertisement
Sign in to follow this  
Q-Parser

Variable number of function args in C

This topic is 4725 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites
Ok, I found some documetation using linux man and found this piece of code:

#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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
A correct use of your function would be: foo("cds", 'a', 121232, "test");

EDIT: too late.

Share this post


Link to post
Share on other sites
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???

Share this post


Link to post
Share on other sites
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;
}


Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!