Passing variable number of arguments around
To pass the ellipses on, you initialize a va_list
as usual and simply pass it to your second function. You don't use va_arg()
. Specifically;
void format_string(char *fmt,va_list argptr, char *formatted_string);
void debug_print(int dbg_lvl, char *fmt, ...)
{
char formatted_string[MAX_FMT_SIZE];
va_list argptr;
va_start(argptr,fmt);
format_string(fmt, argptr, formatted_string);
va_end(argptr);
fprintf(stdout, "%s",formatted_string);
}
Variable number of arguments in C programmng
I want to use functions like sum(1,2,3) should return 6. i.e, no counter should be there
You could define a sentinel. In this case 0
might make sense.
/* Sums up as many int as required.
Stops adding when seeing the 1st 0. */
int sum(int i, ...)
{
int s = i;
if (s)
{
va_list ap;
va_start(ap, i);
/* Pull the next int from the parameter list and if it is
equal 0 leave the while-loop: */
while ((i = va_arg(ap, int)))
{
s += i;
}
va_end(ap);
}
return s;
}
Call it like this:
int sum(int i, ...);
int main(void)
{
int s = sum(0); /* Gives 0. */
s = sum(1, 2, 3, 0); /* Gives 6. */
s = sum(-2, -1, 1, 2, 0); /* Gives 0. */
s = sum(1, 2, 3, 0, 4, 5, 6); /* Gives 6. */
s = sum(42); /* Gives undefined behaviour! */
}
The sum()
function alternatively could also look like this (but would do one useless addition of 0):
/* Sums up as many int as required.
Stops adding when seeing the 1st 0. */
int sum(int i, ...)
{
int s = i;
if (s)
{
va_list ap;
va_start(ap, i);
/* Pull the next int from the parameter list and if it is
equal 0 leave the do-loop: */
do
{
i = va_arg(ap, int);
s += i;
} while (i);
va_end(ap);
}
return s;
}
passing variable number of arguments
Here is an example:
#include <stdlib.h>
#include <stdarg.h>
#include <stdio.h>
int maxof(int, ...) ;
void f(void);
int main(void){
f();
exit(EXIT SUCCESS);
}
int maxof(int n_args, ...){
register int i;
int max, a;
va_list ap;
va_start(ap, n_args);
max = va_arg(ap, int);
for(i = 2; i <= n_args; i++) {
if((a = va_arg(ap, int)) > max)
max = a;
}
va_end(ap);
return max;
}
void f(void) {
int i = 5;
int j[256];
j[42] = 24;
printf("%d\n", maxof(3, i, j[42], 0));
}
c++ passing variable number of arguments to a function
With C++11 this is actually possible and I had a quite similar problem. Have a look at the following:
Generic template for calling function with vector elements
Passing variable arguments to another function that accepts a variable argument list
You can't do it directly; you have to create a function that takes a va_list
:
#include <stdarg.h>
static void exampleV(int b, va_list args);
void exampleA(int a, int b, ...) // Renamed for consistency
{
va_list args;
do_something(a); // Use argument a somehow
va_start(args, b);
exampleV(b, args);
va_end(args);
}
void exampleB(int b, ...)
{
va_list args;
va_start(args, b);
exampleV(b, args);
va_end(args);
}
static void exampleV(int b, va_list args)
{
...whatever you planned to have exampleB do...
...except it calls neither va_start nor va_end...
}
C++11 variable number of arguments, same specific type
A possible solution is to make the parameter type a container that can be initialized by a brace initializer list, such as std::initializer_list<int>
or std::vector<int>
. For example:
#include <iostream>
#include <initializer_list>
void func(std::initializer_list<int> a_args)
{
for (auto i: a_args) std::cout << i << '\n';
}
int main()
{
func({4, 7});
func({4, 7, 12, 14});
}
Can you pass a varying number of arguments of the same type to a function without using arrays?
You can use a recursive template function.
#include <iostream>
template <typename First>
void f(First&& first) {
std::cout << first << std::endl;
}
template <typename First, typename... Rest>
void f(First&& first, Rest&&... rest) {
f(std::forward<First>(first));
f(std::forward<Rest>(rest)...);
}
int main() {
f(6,7,8,9,10);
}
Related Topics
How Can Std::Unique_Ptr Have No Size Overhead
Why Does 'Int ;' Compile Fine in C, But Not in C++
Request for Member '...' Is Ambiguous in G++
Cannot Convert 'Const Char*' to 'Lpcwstr {Aka Const Wchar_T*}'
Base Pointer to Array of Derived Objects
How to Make Gcc Warn on Passing Too-Wide Types to Functions
Why Doesn't C++ Move Construct Rvalue References by Default
How to Use a String as a Variable Name in C++
Differencebetween Using a Struct with Two Fields and a Pair
Why Do String Literals (Char*) in C++ Have to Be Constants
Does C++ Contain the Entire C Language
Windows Unicode C++ Stream Output Failure
How to Overwrite Only Part of a File in C++
How to Show Enter Password in the Form of Asterisks(*) on Terminal