how do i write a macro that performs swapping among 3 variables taken as parameters?
macro problem
Collapse
X
-
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
AdministratorComment
-
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:
Code:#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]);
JosComment
-
Originally posted by JosAHNo need to bother: you can't swap two variables by using a cpp macro
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.Comment
-
Originally posted by JosAHSimply see to what that macro expands; it doesn't work; nothing pedantic about it.
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.Comment
-
Originally posted by BanfaNo, 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.
counter example and you can go back to sleep again: the example showed
that it can't be done so why bother ;-)
kind regards,
JosComment
Comment