473,320 Members | 1,810 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

macro problem

21
how do i write a macro that performs swapping among 3 variables taken as parameters?
Oct 29 '08 #1
21 3723
donbock
2,426 Expert 2GB
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.
Oct 29 '08 #2
stdvu
21
the problem is that the question states exactly the same way as i posted.
Oct 29 '08 #3
donbock
2,426 Expert 2GB
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.
Oct 29 '08 #4
Banfa
9,065 Expert Mod 8TB
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
Oct 29 '08 #5
stdvu
21
I was not trying to be rude :( I was stating the problem...... i will check with the teacher straight away ..... i myself am confused ..... :(
Nov 3 '08 #6
stdvu
21
Ok my teacher replied that perform swap among 2 variables
Nov 4 '08 #7
Banfa
9,065 Expert Mod 8TB
What really? The macro takes 3 variables but must swap between 2 of them? Which 2 of the 3?
Nov 4 '08 #8
stdvu
21
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.
Nov 4 '08 #9
JosAH
11,448 Expert 8TB
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
Nov 4 '08 #10
Banfa
9,065 Expert Mod 8TB
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.
Nov 4 '08 #11
JosAH
11,448 Expert 8TB
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
Nov 4 '08 #12
stdvu
21
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..
Nov 4 '08 #13
Banfa
9,065 Expert Mod 8TB
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.
Nov 4 '08 #14
JosAH
11,448 Expert 8TB
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
Nov 4 '08 #15
Banfa
9,065 Expert Mod 8TB
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.
Nov 4 '08 #16
JosAH
11,448 Expert 8TB
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
Nov 4 '08 #17
stdvu
21
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);
};
Nov 5 '08 #18
JosAH
11,448 Expert 8TB
Try and run it and see for yourself.

kind regards,

Jos
Nov 5 '08 #19
Banfa
9,065 Expert Mod 8TB
You may wish to add statements to print the value of x and y both before and after you call your macro
Nov 5 '08 #20
stdvu
21
hmmm yeah ..... shud do that thanks a lot for help :)
Nov 5 '08 #21
donbock
2,426 Expert 2GB
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.
Nov 5 '08 #22

Sign in to post your reply or Sign up for a free account.

Similar topics

25
by: Andrew Dalke | last post by:
Here's a proposed Q&A for the FAQ based on a couple recent threads. Appropriate comments appreciated X.Y: Why doesn't Python have macros like in Lisp or Scheme? Before answering that, a...
699
by: mike420 | last post by:
I think everyone who used Python will agree that its syntax is the best thing going for it. It is very readable and easy for everyone to learn. But, Python does not a have very good macro...
1
by: Giganews | last post by:
I have an Access 97 database in which I am running an Excel macro through automation. The macro in Excel is as follows: Worksheets("Sheet1").Protect Password:="****", DrawingObjects:=True,...
8
by: David | last post by:
Hi, I am using header file for driver so Can I define macro to call Kernel function ? What are the generatl rules for defining macro. - David
20
by: Srinivas Mudireddy | last post by:
Hi, I am facing a problem with trying to conditionally compile inside a macro. I have a macro #define SET_VAL(x, y) ((x) = *(y)) But the problem is y can be NULL in which case I want to set...
10
by: Praveen.Kumar.SP | last post by:
Hi Could anyone solve the problem for the code below The Code: #include "stdio.h" #include "iostream.h" void Temp( int a, char* str,...)
6
by: Takeadoe | last post by:
Dear NG, Can someone assist me with writing the little code that is needed to run an update table query each time the database is opened? From what I've been able to glean from this group, the...
8
by: Net | last post by:
Hi Please help. I have a database which requires a message to come up when certain part numbers are added to it. I have solved part of this by using a conditional macro eg = 54125 and using a msg...
6
by: jason | last post by:
Hi, I learned my lesson about passing pointers, but now I have a question about macros. Why does the function work and the MACRO which is doing the same thing on the surface, does not work in...
36
by: sh.vipin | last post by:
how to make large macro paste the code as it is Problem Explanation '-- For example in the program below /* a.c - starts here */ #define DECL_VARS() \ unsigned int a0;\ unsigned int a1;\...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.