Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   Mapping a macro with variable number of arguments to a variadic function (http://www.velocityreviews.com/forums/t445422-mapping-a-macro-with-variable-number-of-arguments-to-a-variadic-function.html)

rashmi 12-05-2006 12:12 PM

Mapping a macro with variable number of arguments to a variadic function
 
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


Dead Loop 12-05-2006 12:41 PM

Re: Mapping a macro with variable number of arguments to a variadic function
 
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. :-)


Tom St Denis 12-05-2006 01:04 PM

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

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


matevzb 12-05-2006 01:06 PM

Re: Mapping a macro with variable number of arguments to a variadic function
 
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


rashmi 12-06-2006 07:48 AM

Re: Mapping a macro with variable number of arguments to a varia
 
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.


rashmi 12-06-2006 08:34 AM

Re: Mapping a macro with variable number of arguments to a varia
 
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.


rashmi 12-07-2006 07:12 AM

Re: Mapping a macro with variable number of arguments to a varia
 
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,...);




All times are GMT. The time now is 04:31 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.