![]() |
|
#1
|
||||
|
||||
SWAP macroI am using this macro for swapping any type of numbers. But I am not able to swap pointers through this. Is there any way I can achieve swapping of pointers also.
CPP / C++ / C Code:
I also tried this CPP / C++ / C Code:
Is there a way I can achieve swapping of pointers and basic data types with single macro. __________________
Gravitation is not responsible for people falling in love. -Albert Einstein |
||||
|
#2
|
|||
|
|||
Re: SWAP macroQuote:
Think of the #define as a simple text substitution macro. Pretend you are the preprocessor. Copy/paste the exact code for the macro into the places where your program tried to use and then replace the macro parameters with the arguments that you used to invoke the macro. You will end up with something like the following, which is what the C compiler will see: CPP / C++ / C Code:
Try to compile this. You will see the same error messages you saw when tried it with the macro in place. You could try something like the following: CPP / C++ / C Code:
The two macro invocations insert something like the following at each point: CPP / C++ / C Code:
This is legal C and C++ code, and actually performs the swaps. [disclaimer] I haven't tried to see if there is any way to abuse this kind of construct, but there very well may places where a swap() function might work and the macro might not. I have never used or tested this in any program other than this example, and, in general, I discourage use of macros. (Actually, for personal reasons, I have an extreme aversion to the promiscuous use of macros.) [/disclaimer] Quote:
Of course you can't use this little trick with anything other than integer data types. Why would you think you could? Regards, Dave |
|
#3
|
||||
|
||||
Re: SWAP macrook i understand where i was going wrong but i couldn't undertsnad one thing
why did you use this CPP / C++ / C Code:
CPP / C++ / C Code:
__________________
Gravitation is not responsible for people falling in love. -Albert Einstein |
|
#4
|
|||
|
|||
Re: SWAP macroQuote:
I'm not sure that it does. I don't propose to test all of the ways that alternatives can go wrong. Remember my disclaimer: I don't recommend any of these macros to anyone, any time, anywhere, for any purpose. I just suggested ways of defining macros and testing by manual substitution of code equivalent to the macro expansions into example programs. The only reason that I responded to the thread at all is that I think it is sometimes useful to examine the function of the preprocessor, and I wanted to follow through with an example that implements the functionality that you indicated you are interested in. If you are not sure how the preprocessor works, you may be able to see the output of the preprocesser, depending on your particular compiler setup. This might be a good way to get a feel for what the C preprocessor really does. Borland: Look in the Borland bin directory (where the Borland tools were installed). In my case, the compiler is "bcc32.exe". There is a file named "cpp32.exe". Now go back to the directory where your source file is located. Suppose the source file is named "z.c". Then (assuming that the Borland bin directory is in the path, and that bcc32.cfg is set up to compile files with bcc32), enter something like the following on the command line: cpp32 z.c This should create a file named z.i Somewhere near the bottom of z.i, there is your source code, except you will see that the macros have been expanded. This is what the compiler would have been working on if you had entered "bcc32 z.c" For Microsoft compilers, you can try entering the following: cl z.c /E > zclpp.out Now look in the resulting file, zclpp.out, for the macro expansion Note that this works for me with Microsoft Visual C++ version 6.0. (It may be necessary to execute the file VCVARS32.BAT (in the Microsoft installation binary directory) to set up system environment variables. I don't know if this works the same with other Microsoft compilers. For GNU compilers, enter the following on the command line: gcc -E z.c >zgccpp.out You should see the expanded macros in file zgccpp.out. Regards, Dave Regards, Dave |
|
#5
|
||||
|
||||
Re: SWAP macroThank You Dave
__________________
Gravitation is not responsible for people falling in love. -Albert Einstein |
Recent GIDBlog
Problems with the Navy (Chiefs) by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| A simple swap program | crystalattice | Python Forum | 0 | 16-Oct-2005 01:02 |
| Sorting arrays need help | doc_watson2007 | C Programming Language | 10 | 18-Nov-2004 22:00 |
| Image swap problems | jefferooo | Web Design Forum | 2 | 28-Nov-2003 08:32 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The