Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ C
#include<stdio.h>
#define SWAP(a, b, c)(c t; t=a, a=b, b=t)
int main()
{
    int x=10, y=20;
    SWAP(x, y, int);
    printf("%d %d\n", x, y);
    return 0;
}
 
why it is not allowing int to be declared?can we declare inside a paranthesis?whether it is allowed?
Posted 17-Jan-13 1:52am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Try this instead
 
#define SWAP(a, b, c) {c t; t=a, a=b, b=t;}
 
I inserted a space, and exchanged parentheses for braces.
  Permalink  
v2
Comments
nv3 at 17-Jan-13 8:12am
   
The space should not be necessary. Other than that you hit it!
 
To questioner: See the std::swap template function which does exactly that without even needing the type argument.
Espen Harlinn at 17-Jan-13 8:39am
   
What happens if your macro is used in this fashion:
if(x < y)
SWAP(x,y,int);
else
y++;
// Hint: It will not compile ...
Mattias Högström at 17-Jan-13 11:05am
   
You are right.
Hey!! Don't call it my macro. I just tried to correct it (but missed making it foolproof).
I hate macros. They are error prone and makes it harder to debug the code.
I would prefer an inlined function over a macro.
 
std::swap is the preferred alternative
 
ps. According to my coding style guideline
Even 1 line if else statements should use braces. :)
Espen Harlinn at 17-Jan-13 14:41pm
   
Relax, IMHO this should not be implemented as a macro :)
 
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Use std::swap[^]
or if you're set on doing this using the preprocessor:
#define SWAP(a,b,c) do {c t=a; a=b; b=t;}while(0)
The do while construct ensures that you can do something like this:
if(x < y)
 SWAP(x,y,int);
else
 y++;
// rest of your code
Without undesired side effects - I would advice you to use std::swap.
 
Best regards
Espen Harlinn
  Permalink  
v2
Comments
H.Brydon at 17-Jan-13 13:39pm
   
Correct and complete... +5 from me.
Espen Harlinn at 17-Jan-13 14:43pm
   
Thank you, Brydon :-D
CPallini at 18-Jan-13 7:41am
   
5.
I would advise the OP to follow your advice.
Espen Harlinn at 18-Jan-13 11:37am
   
Thank you, Carlo :-D
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

I am not sure where you are going with these questions, but I would strongly recommend you get hold of a decent book on C++ and study it. Trying to solve programming problems with preprocessor macros is a bad idea and can lead to many difficult to diagnose problems. Use the language to write your code using the proper tools and constructs.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Take a look Click[^]
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Advertise | Privacy | Mobile
Web02 | 2.8.141022.2 | Last Updated 17 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100