Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Mapping a macro with variable number of arguments to a variadic function

Reply
Thread Tools

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

 
 
rashmi
Guest
Posts: n/a
 
      12-05-2006
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

 
Reply With Quote
 
 
 
 
Dead Loop
Guest
Posts: n/a
 
      12-05-2006
rashmi wrote:
> 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.

 
Reply With Quote
 
 
 
 
Tom St Denis
Guest
Posts: n/a
 
      12-05-2006

rashmi wrote:
> 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

 
Reply With Quote
 
matevzb
Guest
Posts: n/a
 
      12-05-2006
Dead Loop wrote:
<snip>
> 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

--
WYCIWYG - what you C is what you get

 
Reply With Quote
 
rashmi
Guest
Posts: n/a
 
      12-06-2006
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.

 
Reply With Quote
 
rashmi
Guest
Posts: n/a
 
      12-06-2006
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.

 
Reply With Quote
 
rashmi
Guest
Posts: n/a
 
      12-07-2006
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,...);


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Call again a variadic function (... variable number of arguments)with same arguments that its variadic wrapper moreau.steve@gmail.com C Programming 3 12-31-2008 07:13 AM
variadic function calling variadic function goldfita@signalsguru.net C Programming 5 05-03-2006 05:23 PM
Using variadic functions within variadic functions pinkfloydhomer@gmail.com C Programming 2 02-27-2006 05:47 AM
Variadic functions calling variadic functions with the argument list, HLL bit shifts on LE processors Ross A. Finlayson C Programming 19 03-10-2005 03:57 AM
is casting a variadic function to a non-variadic one legal? Colin Walters C Programming 2 02-13-2004 10:55 PM



Advertisments