di**********@yahoo.com said:
Hi.If I define a macro which contains a variable declaration,then
during expansion the declaration will not be placed at the beginning of
the program,but somwhere within it.How can I overcome it?
By not declaring the variable within the macro.
But if you must do so, then you can use {} to create a local scope for the
variable:
#define swap(arithmetictype, x, y) \
{ arithmetictype temp = x; x = y; y = temp; }
Usage:
int i = 42;
int j = 6;
swap(int, i, j)
The variable ceases to exist at the point that the } is reached. Although it
is not, strictly speaking, necessary, some people like to wrap a dummy
do-while around the compound statement, so that they can see a nice normal
semicolon at the end of the macro. Other people, such as myself, prefer not
to play such games with macros at all, and would rather write a function to
do whatever-it-is instead.
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)