if I have a macro defined as
The above would work if a,b were numbers and 'c' was a data type like int.but it wont work for float * or any other pointer type.it will give a compile error ,why?Code:#define swap(a,b,c) c t; t=a,a=b,b=t;
cheers!
if I have a macro defined as
The above would work if a,b were numbers and 'c' was a data type like int.but it wont work for float * or any other pointer type.it will give a compile error ,why?Code:#define swap(a,b,c) c t; t=a,a=b,b=t;
cheers!
You would need to show an example use of "it doesn't work".
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge
I think that it should work in quite a few situations, but to enable it to work in more situations:Originally Posted by MK27
Code:#define swap(a,b,c) do { c t; t=a,a=b,b=t; } while (0)
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
How would I call that then? I almost never use macros, so I am interested in the syntax. But if I try:
Where x y z are ints, I get:Code:swap(x,y,z);
error: ‘t’ undeclared (first use in this function)
for both versions of this. Of course, this will work:
Code:#define swap2(a,b,t) {t=a,a=b,b=t;}
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge
swap generally involves 2 variables. In the macro, the third parameter specifies the type, so if x and y are ints, you would call it swap(x, y, int)
Eh, eklavya8 described an example: "The above would work if a,b were numbers and 'c' was a data type like int."Originally Posted by MK27
Thus the example would be:
Code:swap(x, y, int);
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Got it, thanks (I was thinking, "t" for temp, and that the OP meant "submit a tmp variable of the correct type")!
So today is my day to learn about writing macros...
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge
MK27, tabstop's statement was in response to your comment about wanting to know where eklavya8's swap macro will work. It was not in response to your comment about XOR swap.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)