How can I pass many arguments to a C function? Assuming that I have this function:
void f(int n, char* a, char* b, ...)
I want an undefined number of char* arguments. How can I do so?
How can I pass many arguments to a C function? Assuming that I have this function:
void f(int n, char* a, char* b, ...)
I want an undefined number of char* arguments. How can I do so?
What you needs is called variable number of argument functions, you can read from : 9.9. Variable numbers of arguments a good and essay tutorial.
A short theory in four points will help you to understand my code:
<stdarg.h>
header file must be included, his introduces a new
type, called a va_list, and three functions that operate on objects
of this type, called va_start, va_arg, and va_end
.va_start:
is a macro to set arg_ptr
to beginning of list ap
of
optional argumentsva_arg:
does is use this saved stack pointer, and extract the
correct amount of bytes for the type providedva_end:
is a macro to reset ap
, After all arguments have been
retrieved, va_end
resets the pointer to NULL.This theory is not enough but below an example (as you required) will help you to understand basic work-flow/ and steps: (read comment for each 4 steps)
//Step1: Need necessary header file
#include <stdarg.h>
void f(int first, char* a, char* b, ...){
va_list ap; // vlist variable
int n; // number
char aa,
int i;
float f;
//print fix numbers of arguments
printf("\n %d, %s, %s\n", first, a, b);
//Step2: To initialize `ap` using right-most argument that is `b`
va_start(ap, b);
//Step3: Now access vlist `ap` elements using va_arg()
n = va_arg(ap, int); //first value in my list gives number of ele in list
while(n--){
aa = (char)va_arg(ap, int); // notice type, and typecast
i = va_arg(ap, int);
f = (float)va_arg(ap, double);
printf("\n %c %d %f \n", aa,i, f);
}
//Step4: Now work done, we should reset pointer to NULL
va_end(ap);
}
int main(){
char* a = "Aoues";
char* b = "Guesmi";
f(2, a, b, 3, 'a', 3, 6.7f, 'b', 5, 5.5f, 'A', 0, 0.1);
// ^ this is `n` like count in variable list
return 1;
}
Who does it runs:
~$ ./a.out
2, Aoues, Guesmi
a 3 6.700000
b 5 5.500000
A 0 0.100000
A brief explanation of my code will be helpful for future users:
char* b
in our function f()
) uses just to
initialized viable list ap
.f()
above fist reads n
value that is 3
(read
comment in main). In f()
, while(n--)
executes for three
time and each time in loop using va_arg()
macro we retrieves three
values.ints
then a double
, Where as I
am sending char, int, float
(notice in main where I call f()).
this is because auto type promote in case of variable argument list.
(read in detail from above lisk)Her is one more useful link from MSDN: va_arg, va_end, va_start.
(let me know if you need more help regarding this)
Use varargs (look this up yourself, the stuff the callee uses to read the args is in stdarg.h
).
Note that there is no way for the callee to automatically determine the number of arguments actually passed. So for example it might be n
, and you just have to trust the caller to get n
right.