macro problem | Newbie | | Join Date: Oct 2008
Posts: 21
| | |
how do i write a macro that performs swapping among 3 variables taken as parameters?
| | Expert | | Join Date: Mar 2008 Location: Naperville, Illinois U.S.
Posts: 828
| | | re: macro problem
Please describe what you want this macro to do. What gets swapped with what? Here's a starting point:
#define MACRO(P1,P2,P3) ...
You can describe the desired behavior in terms of P1, P2, and P3.
| | Newbie | | Join Date: Oct 2008
Posts: 21
| | | re: macro problem
the problem is that the question states exactly the same way as i posted.
| | Expert | | Join Date: Mar 2008 Location: Naperville, Illinois U.S.
Posts: 828
| | | re: macro problem
You'll have to ask the teacher for clarification.
The intent would be obvious if you were asked to swap two variables. "Swapping among three variables" could mean any number of things.
|  | AdministratorVoR | | Join Date: Feb 2006 Location: South West UK
Posts: 6,148
| | | re: macro problem
Yes unfortunately there are (at least) 6 different ways to do swapping among 3 variables A, B and C all producing different results so if you don't indicate which one (result) you are trying implement it is not going to be possible for us to help you.
The experts and members of this community give their help and time free of charge. You have no right to that help and being rude and displaying a bad attitude is going to make it more likely that you do not receive help.
The question would not have been asked if it was not necessary to know its answer in order to help you.
Banfa
Administrator
| | Newbie | | Join Date: Oct 2008
Posts: 21
| | | re: macro problem
I was not trying to be rude :( I was stating the problem...... i will check with the teacher straight away ..... i myself am confused ..... :(
| | Newbie | | Join Date: Oct 2008
Posts: 21
| | | re: macro problem
Ok my teacher replied that perform swap among 2 variables
|  | AdministratorVoR | | Join Date: Feb 2006 Location: South West UK
Posts: 6,148
| | | re: macro problem
What really? The macro takes 3 variables but must swap between 2 of them? Which 2 of the 3?
| | Newbie | | Join Date: Oct 2008
Posts: 21
| | | re: macro problem
I mailed my teacher to ask him what he meant and he answere me briefly saying :
dear student,
you can do swap operation on two values.
|  | Expert | | Join Date: Mar 2007
Posts: 10,611
| | | re: macro problem
No need to bother: you can't swap two variables by using a cpp macro; essentially
the macro processor uses a by name parameter passing mechanism which is
well known for the fact that you can't swap two variables using that mechanism.
Have a look: -
#define swap(t, a, b) { t x= a; a= b; b= x; }
-
...
-
int a[3] = { 2, 0, 1 };
-
int i= 1;
-
...
-
swap(int, i, a[i]);
-
kind regards,
Jos
|  | AdministratorVoR | | Join Date: Feb 2006 Location: South West UK
Posts: 6,148
| | | re: macro problem Quote:
Originally Posted by JosAH No need to bother: you can't swap two variables by using a cpp macro Personally I think you are being just a little pedantic (if correct).
stdvu ignoring Jos counter example of how things can go horribly wrong I suggest you may be look at a writing a macro to swap 2 integer (int) variables which is probably what your teacher is asking for.
It isn't unusual for teachers assign simplistic assignments without considering the full implications of what they are asking.
|  | Expert | | Join Date: Mar 2007
Posts: 10,611
| | | re: macro problem Quote:
Originally Posted by Banfa Personally I think you are being just a little pedantic (if correct). Simply see to what that macro expands; it doesn't work; nothing pedantic about it.
kind regards,
Jos
| | Newbie | | Join Date: Oct 2008
Posts: 21
| | | re: macro problem
ok so you mean i simply need to swap 2 variables (int) without going into complexity ....... that is just write a simple program that is defined as macro right??? I will post the code, do temme whether I am correct or not..
|  | AdministratorVoR | | Join Date: Feb 2006 Location: South West UK
Posts: 6,148
| | | re: macro problem Quote:
Originally Posted by JosAH Simply see to what that macro expands; it doesn't work; nothing pedantic about it. No, no I understand that what you have written doesn't work and demonstrates that that type of macro doesn't work. But that is because of your use of an array and I am sure that is not what stdvu's teacher had in mind, hence "pedantic (if correct)".
It is possible to create a macro that, for instance, swaps the values of 2 int variables (as opposed to an int and an array variable indexed by that int). It is not possible to create a macro that works in all cases.
|  | Expert | | Join Date: Mar 2007
Posts: 10,611
| | | re: macro problem Quote:
Originally Posted by Banfa No, no I understand that what you have written doesn't work and demonstrates that that type of macro doesn't work. But that is because of your use of an array and I am sure that is not what stdvu's teacher had in mind, hence "pedantic (if correct)".
It is possible to create a macro that, for instance, swaps the values of 2 int variables (as opposed to an int and an array variable indexed by that int). It is not possible to create a macro that works in all cases. Ok, case closed; the mathematical lazy approach isn't appreciated: one
counter example and you can go back to sleep again: the example showed
that it can't be done so why bother ;-)
kind regards,
Jos
|  | AdministratorVoR | | Join Date: Feb 2006 Location: South West UK
Posts: 6,148
| | | re: macro problem
I would certainly agree with that. Unfortunately students don't tend to win many marks from their teachers for providing such proofs as opposed to the answer the teacher was angling for.
|  | Expert | | Join Date: Mar 2007
Posts: 10,611
| | | re: macro problem Quote:
Originally Posted by Banfa I would certainly agree with that. Unfortunately students don't tend to win many marks from their teachers for providing such proofs as opposed to the answer the teacher was angling for. That reminds me of a little joke: a mathematician and a physicist were asked
the question: if you have a gas stove, an empty kettle, a water tap and a box of matches,
how do you boil water?
They both give the same sensible answer.
Then they were asked: you have a kettle filled with water on the stove, how do
you boil water?
The physicist goes: I simply light the gas and wait until the water boils in the kettle.
The mathematician: I empty the kettle and the problem is reduced to the previous problem.
kind regards,
Jos
| | Newbie | | Join Date: Oct 2008
Posts: 21
| | | re: macro problem
well how about this code ......... temme if its going to work or not ....... i need to submit this tagging macro problem ....... here is the code
#define SWAP(a, b) {a ^= b; b ^= a; a ^= b};
int main()
{
int x=3;
int y=6;
swap (x,y);
};
|  | Expert | | Join Date: Mar 2007
Posts: 10,611
| | | re: macro problem
Try and run it and see for yourself.
kind regards,
Jos
|  | AdministratorVoR | | Join Date: Feb 2006 Location: South West UK
Posts: 6,148
| | | re: macro problem
You may wish to add statements to print the value of x and y both before and after you call your macro
| | Newbie | | Join Date: Oct 2008
Posts: 21
| | | re: macro problem
hmmm yeah ..... shud do that thanks a lot for help :)
| | Expert | | Join Date: Mar 2008 Location: Naperville, Illinois U.S.
Posts: 828
| | | re: macro problem
There are a number of limitations when you try to make a macro act like a function. As Jos has already pointed out, cross-referential arguments typically lead to disaster. So do explicit side-effects, like swap(int,a++,b--). Function-like macros should be comprehensively commented to point out limitations like these.
A common idiom is to define a multi-statement macro within a do{...}while(0) block rather than naked braces as shown in reply #10. Notice the terminating semicolon is not included within the macro definition. This idiom encourages the macro call to be semi-coloned like any other statement without altering how it acts if it is within nested unbraced if statements.
Suppose you have a function that you sometimes want to override with a macro. You might want to do this to gain some execution speed at the cost of error checking and the well-known limitations of function-like macros. (This is done in the curses library.) My first suggestion is to reconsider -- perhaps inline functions will work for you. If you choose to proceed then you want to do the following. Parentheses around the function name in the prototype and function definition prevent the function name from being replaced by the macro expansion. A user who includes the header file will get the macro expansion when calling func unless they follow the #include with #undef func", in which case they call the function. Too tricky by half. - header file:
-
void (func)(...);
-
#define func(...) ...
-
C file:
-
void (func)(...) {...}
On the other hand, with all the limitations of function-like macros ... a good argument can be made for not getting too good at writing them.
|  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,223 network members.
|