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
|
|
0
|
|
|
|
Reply
|
nsrashmi (17)
|
12/5/2006 12:12:08 PM |
|
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. :-)
|
|
0
|
|
|
|
Reply
|
foreachelement (10)
|
12/5/2006 12:41:05 PM
|
|
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
|
|
0
|
|
|
|
Reply
|
tomstdenis (355)
|
12/5/2006 1:04:01 PM
|
|
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
|
|
0
|
|
|
|
Reply
|
matevzb (155)
|
12/5/2006 1:06:39 PM
|
|
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.
|
|
0
|
|
|
|
Reply
|
nsrashmi (17)
|
12/6/2006 7:48:00 AM
|
|
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.
|
|
0
|
|
|
|
Reply
|
nsrashmi (17)
|
12/6/2006 8:34:19 AM
|
|
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,...);
|
|
0
|
|
|
|
Reply
|
nsrashmi (17)
|
12/7/2006 7:12:07 AM
|
|
|
6 Replies
44 Views
(page loaded in 2.435 seconds)
Similiar Articles: How to simulate variadic templates? - comp.lang.c++.moderated ...... 2B%2B0x#Variadic_templates I would need a template with variable number of parameters ... NEW_MAX_ARGUMENTS_NUMBER macro). Now you can notice that your 'New' function ... Floating Point and printf() - comp.lang.asm.x86... printf() is a variadic function and ... not agree with the number of parameters, the behavior is undefined. If the function ... printf() is a variadic ... Microsoft (R) Macro ... Neatest way to get the end pointer? - comp.lang.c(Oh and by the way, I wouldn't use a macro ... of course; let's assume that it's variadic and that its last argument ... math. > I also like to modify function parameters > at ... Could anyone give me the spice-mode.el - comp.emacsHi, All I am new to *NIX and I am thinking of writing spice code under Emacs. However, I have no idea of Emacs Lisp. Hence, I could not write a packa... Const constructor - comp.lang.c++.moderatedAnd in fact there have been > a number of nice ... A variadic argument U&&... matches a copy-from-lvalue ... These two function parameters had different const ... Help needed: read 3-dimensional array from a MAT-file in Fortran ...... g., this line in the code loads an mxArray variable from ... lib libmex.lib libmat.lib /implib:"%LIB_NAME%.x" /MAP ... from a MAT-file in Fortran ... ... fortran Random number ... LSA Disk Sector Read - using int 13h ah 42 (extended read) - comp ...Sixth, your compiler may have C structures which map to ... 10h or 18h) 01h BYTE reserved (0) 02h WORD number of ... that their use of the address operator within the macro ... improve strlen - comp.lang.asm.x86... int length = x.length(); This can be a simple variable ... block-copied into the slot in the left sidehand argument ... measurements would still be all over the map based on ... How to check whether malloc has allocated memory properly in case ...Same applies to any utility function that you use widely ... standard) that one could read in, store in a variable ... MORTRAN2 uses the first card of the macro file as its ... Variadic function - Wikipedia, the free encyclopediaIn computer programming, a variadic function is a function of indefinite arity, i.e., one which accepts a variable number of arguments. ... implementation of prog1, a macro ... The C Preprocessor - delorie software3.6 Variadic Macros . A macro can be declared to accept a variable number of arguments much as a function can. The syntax for defining the macro is similar to that of ... 7/30/2012 6:11:22 AM
|