473,326 Members | 2,124 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

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

Dec 5 '06 #1
6 4022
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. :-)

Dec 5 '06 #2

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

Dec 5 '06 #3
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

Dec 5 '06 #4
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.

Dec 6 '06 #5
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.

Dec 6 '06 #6
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,...);
Dec 7 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

699
by: mike420 | last post by:
I think everyone who used Python will agree that its syntax is the best thing going for it. It is very readable and easy for everyone to learn. But, Python does not a have very good macro...
7
by: A. Saksena | last post by:
Hi all, Is it possible to write a function or a macro in C++, which is capable of accepting any number of arguments. To give an example, the following should be possible: - ...
10
by: The Directive | last post by:
I read the C FAQ question on passing a variable number of arguments, but it didn't help. The example assumes all arguments are of the same type. I want to create a function "trace" that can be...
10
by: Praveen.Kumar.SP | last post by:
Hi Could anyone solve the problem for the code below The Code: #include "stdio.h" #include "iostream.h" void Temp( int a, char* str,...)
12
by: Laurent Deniau | last post by:
I was playing a bit with the preprocessor of gcc (4.1.1). The following macros expand to: #define A(...) __VA_ARGS__ #define B(x,...) __VA_ARGS__ A() -nothing, *no warning* A(x) -x ...
148
by: onkar | last post by:
Given the following code & variable i . int main(int argc,char **argv){ int i; printf("%d\n",i); return 0; } here i is allocated from bss or stack ?
37
by: junky_fellow | last post by:
hi guys, Can you please suggest that in what cases should a macro be preferred over inline function and viceversa ? Is there any case where using a macro will be more efficient as compared to...
6
by: Ashwani | last post by:
consider A is of type char *A there is this function X which takes variable number of "char * " arguments, so X can be called like X (A) or X(A, A) or X(A, A, A) and so on I have to call...
9
by: Two-Horned Unicorn | last post by:
The following code compiles and works as expected when compiled with gcc. /* empty-args.c */ #include <stdio.h> #define foo(x,y) puts("bar: x=\"" #x "\"; y=\"" #y "\"") #define bar(x) ...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.