macros question #2

A

Andrew Arro

first of all i would like to thank Alexander Bartolich and Richard
Heathfield for answering my
"macros question" #1

another macros question now:

how could i define a macros with variable number of parameters? i want
to create



void printfatal(char *file, int line, char *format, ...);


so i wanna define a macros wich i could call like

fatal("%d %s", var1, var2);

and which would be preprocessed into

printfatal(__FILE__, __LINE__, "%d %s", var1, var2);

is that possible?
 
K

Kevin D. Quitt

how could i define a macros with variable number of parameters?
fatal("%d %s", var1, var2);
printfatal(__FILE__, __LINE__, "%d %s", var1, var2);

#define fatal( s, ... ) printfatal( __FILE__, __LINE__, __VA_ARGS__ )

Assuming your compiler handles C99.
 
E

E. Robert Tisdale

Kevin said:
#define fatal( s, ... ) printfatal( __FILE__, __LINE__, __VA_ARGS__ )

#define fatal( s, ... ) printfatal( __FILE__, __LINE__, s, __VA_ARGS__ )
 
A

Andrew Arro

E. Robert Tisdale said:
#define fatal( s, ... ) printfatal( __FILE__, __LINE__, s, __VA_ARGS__ )

thanx guys, actualy the best option i think is

#define fatal(... ) printfatal( __FILE__, __LINE__, __VA_ARGS__ )

this case
fatal("message")

also works

thanx again!
 
D

Dave Thompson

how could i define a macros with variable number of parameters? i want
to create



void printfatal(char *file, int line, char *format, ...);


so i wanna define a macros wich i could call like

fatal("%d %s", var1, var2);

and which would be preprocessed into

printfatal(__FILE__, __LINE__, "%d %s", var1, var2);

is that possible?

In addition to the answer already given for C99, which isn't yet
widely implemented, you can do almost but not quite the same thing in
GCC even in older versions.

In the absence of either of those, AFAIK the closest you can come is a
pseudoCurrying hack:

#define fatal /*NOT functionlike*/ \
printfileline(__FILE__,__LINE__), printf /* gets "arguments" */
/* or instead of printf other appropriate variadic function */

or
#define fatal printfilelinethenprintf(__FILE__,__LINE__) which is
declared to return a function pointer and actually returns a pointer
to printf or whatever, which then gets invoked the arguments.

- David.Thompson1 at worldnet.att.net
 

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

No members online now.

Forum statistics

Threads
474,141
Messages
2,570,817
Members
47,362
Latest member
ChandaWagn

Latest Threads

Top