Mapping a macro with variable number of arguments to a variadic function

R

rashmi

Hello All,
Can we map a MACRO with variable number of arguments to a function with
variable number of arguments?
Please help me in finding out how this could be done ?
for eg:
#define MY_MACRO(int mid,int mlevel,...)
my_func(mid,mlevel,format,##args)
void my_func(int mid,int mlevel,char *format,....)
{
va_list ap;
va_start(ap,format);
vprintf(format,ap);
va_end(ap);
}

int main()
{
int a=10;
MY_MACRO(1,1,"hello world %d\n",a);
return 0;
}
when i compile this code I get this error:
"test2.c:23: warning: passing arg 3 of `my_func' makes pointer from
integer without a cast"

Thanks in advance ,
Rashmi
 
D

Dead Loop

rashmi said:
Hello All,
Can we map a MACRO with variable number of arguments to a function with
variable number of arguments?
Please help me in finding out how this could be done ?
for eg:
#define MY_MACRO(int mid,int mlevel,...)
my_func(mid,mlevel,format,##args)
void my_func(int mid,int mlevel,char *format,....)
{
va_list ap;
va_start(ap,format);
vprintf(format,ap);
va_end(ap);
}

int main()
{
int a=10;
MY_MACRO(1,1,"hello world %d\n",a);
return 0;
}

I don't think we can use macro in this way and
I usually define several macros
e.g. TRACE_EVENT(e), TRACE_EVENT_1P(e, p1),
TRACE_EVENT_2P(e, p1, p2)...
to deal with this situation. :)
 
T

Tom St Denis

rashmi said:
Hello All,
Can we map a MACRO with variable number of arguments to a function with
variable number of arguments?
Please help me in finding out how this could be done ?

#define printk(...) fprintf(stderr, __VA_ARGS__);

With GCC (I dunno if that's actually part of the C spec).

Tom
 
M

matevzb

Dead Loop wrote:
I don't think we can use macro in this way and
I usually define several macros
e.g. TRACE_EVENT(e), TRACE_EVENT_1P(e, p1),
TRACE_EVENT_2P(e, p1, p2)...
to deal with this situation. :)

C99, 6.9.3 Macro replacement
<snip>
12 If there is a ... in the identifier-list in the macro definition,
then the trailing arguments, including any separating comma
preprocessing tokens, are merged to form a single item: the variable
arguments. The number of arguments so combined is such that, following
merger, the number of arguments is one more than the number of
parameters in the macro definition excluding the ...)

Plus, this has been available as a gcc extension for years
 
R

rashmi

but I see only those examples the macros which are being mapped to standard
functions like printf or fprintf can it be mapped to user defined variadic
functions also.
 
R

rashmi

but I see only those examples the macros which are being mapped to standard
functions like printf or fprintf can it be mapped to user defined variadic
functions also.
 
R

rashmi

Thanks for your reply!!
it is possible to map variadic macro to a variadic function
#define TEST_LOG2(m,a,msg,...) test_fn(m,a,msg,__VA_ARGS__)
int test_fn(int m,int i,char *msg,...);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top