GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 15-Jan-2006, 03:08
alcoholic's Avatar
alcoholic alcoholic is offline
Member
 
Join Date: Nov 2005
Posts: 170
alcoholic is on a distinguished road

SWAP macro


I 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:
#include <stdio.h>

#define SWAP(a,b,c) c t;t=a;a=b;b=t;

int main(){
	double x=10,y=20;
	/*double *p,*q;
	p=&x;
	q=&y;
	SWAP(*p,*q,double*);*/
	SWAP(x,y,double);
	printf("%lf  %lf",x,y);
	return 0;
}

I also tried this
CPP / C++ / C Code:
#define SWAP(a, b)  a ^= b; b ^= a; a ^= b; 
I was able to swap pointers and integers through this but was not able to do for floats and double through this.
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  
Old 15-Jan-2006, 09:25
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,218
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: SWAP macro


Quote:
Originally Posted by alcoholic
I 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:
#include <stdio.h>

#define SWAP(a,b,c) c t;t=a;a=b;b=t;

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:
#include <stdio.h>

#define SWAP(a,b,c) c t;t=a;a=b;b=t;

int main(){
  double x=10,y=20;
  double *p,*q;
  p=&x;
  q=&y;
  /* this is what SWAP(p,q,double*) inserts into the code at this point */
  double* t;t=*p;*p=*q;*q=t;;

  /* this is what SWAP(x,y,double) inserts into the code at this point */
  double t;t=x;x=y;y=t;;
  printf("%lf  %lf",x,y);
  return 0;
}



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:
#include <stdio.h>

#define SWAP(a,b,c) {c t;t=(a);(a)=(b);(b)=t;}

int main(){
	double x = 10;
        double y = 20;
	double *p, *q;
	p = &x;
	q = &y;
        printf("p and q are pointers to double\n");
        printf("before SWAP: p = %p, *p = %f, q = %p, *q = %f\n", p, *p, q, *q);
	SWAP(p, q, double *);
        printf("after  SWAP: p = %p, *p = %f, q = %p, *q = %f\n", p, *p, q, *q);

        printf("\n");

        printf("x and y are doubles\n");
        printf("before SWAP: x = %f, y = %f\n", x, y);
	SWAP(x, y, double);
        printf("after  SWAP: x = %f, y = %f\n", x, y);

	return 0;
}

The two macro invocations insert something like the following at each point:

CPP / C++ / C Code:
 {double * t;t=(p);(p)=(q);(q)=t;};
.
.
.
 {double t;t=(x);(x)=(y);(y)=t;};
.
.
.

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:
Originally Posted by alcoholic
I also tried this
CPP / C++ / C Code:
#define SWAP(a, b)  a ^= b; b ^= a; a ^= b; 
I was able to swap pointers and integers through this but was not able to do for floats and double through this.

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  
Old 15-Jan-2006, 10:09
alcoholic's Avatar
alcoholic alcoholic is offline
Member
 
Join Date: Nov 2005
Posts: 170
alcoholic is on a distinguished road

Re: SWAP macro


ok i understand where i was going wrong but i couldn't undertsnad one thing
why did you use this
CPP / C++ / C Code:
#define SWAP(a,b,c) {c t;t=(a);(a)=(b);(b)=t;}
instead of this
CPP / C++ / C Code:
#define SWAP(a,b,c) {c t;t=a;a=b;b=t;}
where does the second version fail
__________________
Gravitation is not responsible for people falling in love.
-Albert Einstein
  #4  
Old 15-Jan-2006, 11:34
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,218
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: SWAP macro


Quote:
Originally Posted by alcoholic
ok i understand where i was going wrong but i couldn't undertsnad one thing
why did you use this
CPP / C++ / C Code:
#define SWAP(a,b,c) {c t;t=(a);(a)=(b);(b)=t;}
instead of this
CPP / C++ / C Code:
#define SWAP(a,b,c) {c t;t=a;a=b;b=t;}
where does the second version fail

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  
Old 15-Jan-2006, 18:39
alcoholic's Avatar
alcoholic alcoholic is offline
Member
 
Join Date: Nov 2005
Posts: 170
alcoholic is on a distinguished road

Re: SWAP macro


Thank You Dave
__________________
Gravitation is not responsible for people falling in love.
-Albert Einstein
 
 

Recent GIDBlogProblems with the Navy (Chiefs) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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

All times are GMT -6. The time now is 23:10.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.