Connecting Tech Pros Worldwide Help | Site Map

macro problem

Newbie
 
Join Date: Oct 2008
Posts: 21
#1: Oct 29 '08
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
#2: Oct 29 '08

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
#3: Oct 29 '08

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
#4: Oct 29 '08

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.
Banfa's Avatar
AdministratorVoR
 
Join Date: Feb 2006
Location: South West UK
Posts: 6,148
#5: Oct 29 '08

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
#6: Nov 3 '08

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
#7: Nov 4 '08

re: macro problem


Ok my teacher replied that perform swap among 2 variables
Banfa's Avatar
AdministratorVoR
 
Join Date: Feb 2006
Location: South West UK
Posts: 6,148
#8: Nov 4 '08

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
#9: Nov 4 '08

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.
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#10: Nov 4 '08

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:

Expand|Select|Wrap|Line Numbers
  1. #define swap(t, a, b) { t x= a; a= b; b= x; }
  2. ...
  3. int a[3] = { 2, 0, 1 };
  4. int i= 1;
  5. ...
  6. swap(int, i, a[i]);
  7.  
kind regards,

Jos
Banfa's Avatar
AdministratorVoR
 
Join Date: Feb 2006
Location: South West UK
Posts: 6,148
#11: Nov 4 '08

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.
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#12: Nov 4 '08

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
#13: Nov 4 '08

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..
Banfa's Avatar
AdministratorVoR
 
Join Date: Feb 2006
Location: South West UK
Posts: 6,148
#14: Nov 4 '08

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.
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#15: Nov 4 '08

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
Banfa's Avatar
AdministratorVoR
 
Join Date: Feb 2006
Location: South West UK
Posts: 6,148
#16: Nov 4 '08

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.
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#17: Nov 4 '08

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
#18: Nov 5 '08

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);
};
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#19: Nov 5 '08

re: macro problem


Try and run it and see for yourself.

kind regards,

Jos
Banfa's Avatar
AdministratorVoR
 
Join Date: Feb 2006
Location: South West UK
Posts: 6,148
#20: Nov 5 '08

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
#21: Nov 5 '08

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
#22: Nov 5 '08

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.
Expand|Select|Wrap|Line Numbers
  1. header file:
  2.     void (func)(...);
  3.     #define func(...) ...
  4. C file:
  5.     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.
Reply


Similar C / C++ bytes