any other method by which i can pass variable no. of arguments to a function :)

Recommended Answers

All 5 Replies

Pass an array.

I usually create a structure (with malloc'd arrays if required) and either define the function with that typedef or as a void pointer.

Void pointers are a powerful way to pass literally anything to a function, but usually in those cases you need to also pass some enum or int describing what you are passing to the function.

any other method by which i can pass variable no. of arguments to a function :)

i know one method is thru va_arg related macros...is there any other method....wot wud u do...if the code skeleton looks something like this...

int sum_fun(int num_arg,...)
{

....
...
...


}


main()
{

printf("%d",sum_fun(4,4,5,6,7));
printf("%d",sum_fun(8,1,2,3,4,5,6,9,10));
...
..
..
}

use va_arg related macros

i know one method is thru va_arg related macros...is there any other method....wot wud u do...if the code skeleton looks something like this...

int sum_fun(int num_arg,...)
{

....
...
...


}


main()
{

printf("%d",sum_fun(4,4,5,6,7));
printf("%d",sum_fun(8,1,2,3,4,5,6,9,10));
...
..
..
}

If you're using the ... notation for variable parameters, the only portable option for processing those parameters is the varargs macros. You could rewrite those macros to do the same thing, but that's silly and strictly tied to the compiler.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.