Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use __VA_ARGS__ inside a C function instead of macro? [duplicate]

Tags:

c

Possible Duplicate:
C/C++: Passing variable number of arguments around

I'm currently using the following macro declared on my C file.

#define COMMON_Print(...) printf (__VA_ARGS__) 

Now that call works just fine, but turns out that I need to be able to create a C function that looks something like this:

void COMMON_Print(...) {         printf (__VA_ARGS__); } 

So that function doesn't work, I get an error

"Error : undefined identifier __VA_ARGS__"

The complexity of my project requires to have a function since it's an interface... So how can I get the parameters ... and pass them to the printf function? Or better what am I doing wrong?

Thanks!

like image 982
Jona Avatar asked Dec 02 '10 20:12

Jona


People also ask

What is __ Va_args __ in C?

__VA_ARGS__ is replaced by all of the arguments that match the ellipsis, including commas between them. The C Standard specifies that at least one argument must be passed to the ellipsis to ensure the macro doesn't resolve to an expression with a trailing comma.

How do you pass a macro in an argument?

To assign a macro that you pass arguments to a button, shape, image, or any object, you first right-click that object and click Assign Macro and then type the name of the macro and the argument, following the pattern described in the above examples, and then click OK. 'show_msg "I clicked a button!"'

What is Variadic function in C?

Variadic functions are functions that can take a variable number of arguments. In C programming, a variadic function adds flexibility to the program. It takes one fixed argument and then any number of arguments can be passed.

What is Va_list in C?

va_list is a complete object type suitable for holding the information needed by the macros va_start, va_copy, va_arg, and va_end. If a va_list instance is created, passed to another function, and used via va_arg in that function, then any subsequent use in the calling function should be preceded by a call to va_end.


1 Answers

Each of the ?printf functions has a corresponding v?printf function which does the same thing but takes a va_list, a variable argument list.

#include <stdio.h> #include <stdarg.h>  void COMMON_Print(char *format, ...) {     va_list args;      va_start(args, format);     vprintf(format, args);     va_end(args); } 

Side note: Notice that va_start takes the name of the last argument before the ... as a parameter. va_start is a macro which does some stack-based magic to retrieve the variable arguments and it needs the address of the last fixed argument to do this.

As a consequence of this, there has to be at least one fixed argument so you can pass it to va_start. This is why I added the format argument instead of sticking with the simpler COMMON_Print(...) declaration.

See: http://www.cplusplus.com/reference/clibrary/cstdio/vprintf/

like image 133
John Kugelman Avatar answered Sep 17 '22 01:09

John Kugelman