|
|
Variable argument number in functions
Hello!
If I was to write a function that takes variable argument number that
would simply call printf with those exact arguments, how do I pass them to
printf?
E.g.
int my_printf (const char *fmt, ...)
{
printf ( ??????????? );
}
Thanks,
Andrej
|
|
0
|
|
|
|
Reply
|
andrej.prsa (18)
|
9/4/2003 9:17:39 AM |
|
Andrej Prsa <andrej.prsa@guest.arnes.si> wrote:
> Hello!
>
> If I was to write a function that takes variable argument number that
> would simply call printf with those exact arguments, how do I pass them to
> printf?
>
> E.g.
>
> int my_printf (const char *fmt, ...)
> {
> printf ( ??????????? );
> }
That's what the vprintf() function is for:
#include <stdarg.h>
int my_printf(const char *fmt, ...)
{
va_list ap;
int r;
va_start(ap, fmt);
r = vprintf(fmt, ap);
va_end(ap);
return r;
}
....and so, you should also write a vmy_printf function, so that your
function can itself be wrapped in the same manner:
int vmy_printf(const char *fmt, va_list ap)
{
/* Put whatever my_printf functionality you want here */
return vprintf(fmt, ap);
}
int my_printf(const char *fmt, ...)
{
va_list ap;
int r;
va_start(ap, fmt);
r = vmy_printf(fmt, ap);
va_end(ap);
return r;
}
- Kevin.
|
|
0
|
|
|
|
Reply
|
Kevin
|
9/4/2003 9:40:57 AM
|
|
On Thu, 4 Sep 2003 11:17:39 +0200, Andrej Prsa wrote
> int my_printf (const char *fmt, ...)
> {
> printf ( ??????????? );
> }
Through va_start() and its relatives. There is a precise example in K&R II -
pg.155, but if you don't have K&RII the algorithm is something like that :
* va_start(ap, fmt) /* ap points to the first unnamed argument */
* use va_arg to move forward to the next argument
* use va_end when you have finished
HTH
D.
--
PGP key 39A40276 at wwwkeys.eu.pgp.net
key fingeprint: 9A0B 61C6 B826 4B73 69BB 972B C5E7 A153 39A4 0276
|
|
0
|
|
|
|
Reply
|
mandas (27)
|
9/4/2003 9:44:05 AM
|
|
Andrej Prsa <andrej.prsa@guest.arnes.si> writes:
> If I was to write a function that takes variable argument number that
> would simply call printf with those exact arguments, how do I pass them to
> printf?
If you had read the FAQ, you would already know.
--
"I don't have C&V for that handy, but I've got Dan Pop."
--E. Gibbons
|
|
0
|
|
|
|
Reply
|
blp (3953)
|
9/4/2003 5:15:50 PM
|
|
|
3 Replies
35 Views
(page loaded in 0.079 seconds)
Similiar Articles: How can I get the name of a variable as function argument in the ...Variable name of function input - MATLAB - MathWorks - MATLAB and ... This MATLAB function returns the workspace variable name corresponding to the argument number argnum. Porting Question (About Nodes and Symbols) - comp.compilers.lcc ...... number. >I want to reduce this, if a function only call functions with 1 >argument, only stack for 1 argument needs to be reserved. >Exception : Functions with variable ... Finding the last arg passed to a function - comp.lang.asm.x86 ...For the fixed number, both sides know in advance how many arguments there will be. For the variable version (used by functions like printf), there has to be some ... Local array variables in functions - comp.lang.awk(POSIX?) "The number of parameters in the function definition ... If fewer arguments are supplied in a function call than are ... trying to pass a stem variable to a function ... Passing va_list by reference to a function - comp.lang.c ...... time, and not always the same argument of the variable argument list. The xprintf() function ... Difference between passing a number and a variable to a subroutine ... ... function returning a comma-separated list - comp.soft-sys.matlab ...Since any MATLAB function can return a variable number of output arguments, MATLAB relies on explicit assignment of these outputs to variables in order to know how many ... Using C and Assembly code: 64Bit Calling convention - comp.lang ...... variable argument functions, the fixed part of a variable argument list may be passed in a completely different fashion than in a fixed argument list with the same number ... "C" calling convention on x86 - comp.lang.asm.x86... pushed onto stack as single DWORDs > Except when dealing with variable-argument functions ... caller fixup only makes sense with functions that take variable numbers of ... Variadic Macros - comp.lang.c++.moderatedVariadic Macros (C++) Variadic macros are function-like macros that contain a variable number of arguments. Functions with two output parameters... - comp.soft-sys.matlab ...... r] = rep4(x) % Given a nonnegative number x, function rep4 ... it looks like the program returns two output arguments ... soft-sys.matlab ... where x is the input variable a ... Variadic function - Wikipedia, the free encyclopediaIn computer programming, a variadic function is a function of indefinite arity, i.e., one which accepts a variable number of arguments. Support for variadic functions ... Lesson 17: Functions with Variable Argument Lists in C and C++ ...Perhaps you would like to have a function that will accept any number of values and then return the average. You don't know how many arguments will be passed in to ... 7/2/2012 3:28:49 AM
|
|
|
|
|
|
|
|
|