Hi, could anyone help me please? i'm not sure how to write a swap(t, x, y) macro that interchange the x and y arguments of type t... :confused:
Thanks;
Printable View
Hi, could anyone help me please? i'm not sure how to write a swap(t, x, y) macro that interchange the x and y arguments of type t... :confused:
Thanks;
Sounds like a homework assignment. Well, why don't you show us what you have so far?
It's not a homework assignment. I just started C programming and i wanted to know how to write functions in macro that can allow any data type that's all.
Well, what do you have? Do you know how to make a macro? If so try something and post and I am sure more people will help you from there.
Well, i just know to write a function like this...
But i'm not sure how to do a swap function...Code:
#define isBigger(a,b) (((a) > (b))? (a):(b))
Well, a swap function typically involves 3 variables; the two you are swapping and one used as a temp store. To perform a swap in code you would(assuming you had three variables named x, y, z and you wanted to swap x and y):
And that would swap the values of variables x and y. Now just attempt to apply that to a macro and you'll be there.Code:z = x
x = y
y = z
i see, so is this correct?
Code:#define swap(t, a, b) (t temp=a; a=b; b=temp;)
yep, just what exactly are you hoping to use, either t or temp. It has to be one or the other. According to you macro declaration it should be t so:
Code:#define swap(t, a, b) (t =a; a=b; b=t;)
sorry, i confussed you. i wanted the t enter in the swap() to be the type t of the a and b arguments. Is it possible to do something like that?
I believe you can only create generic types like that in C++ using templates. For you problem I would just have the programmer pass the macro three variables like so:
I will look around some more and see if I can find a reference to templating in C but I do not believe it can be done like how you want it to.Code:#include <stdio.h>
#include <string.h>
#define swap(x, y, z) (z) = (x); (x) = (y); (y) = (z);
int main()
{
int x = 0, y = 1, z;
float a = 2.0, b = 3.5, c;
printf("The value before swap of x= %i, y= %i\n", x, y);
swap(x, y, z);
printf("The value after swap of x= %i, y= %i\n", x, y);
printf("The value before swap of a= %f, b= %f\n", a, b);
swap(a, b, c);
printf("The value after swap of a= %f, b= %f\n", a, b);
getchar();
return 0;
}
Thanks for your help :)
http://www.eskimo.com/~scs/C-faq/q10.3.html
http://www.eskimo.com/~scs/C-faq/q10.4.html
Code:#include <stdio.h>
#define swap(t,x,y) \
do { \
t a = (x); \
(x) = (y); \
(y) = a; \
} while(0)
int main(void)
{
int i = 42, j = -1, k = 9, *p = &i, *q = &j;
float f = 1.23, g = 4.56;
printf("i = %d, j = %d | k = %d | f = %g, g = %g | p = %p, q = %p\n",
i, j, k, f, g, (void*)p, (void*)q);
swap(int, i, j);
swap(float, f, g);
swap(int, k, k);
if ( k )
swap(int*, p, q);
else
puts("message");
printf("i = %d, j = %d | k = %d | f = %g, g = %g | p = %p, q = %p\n",
i, j, k, f, g, (void*)p, (void*)q);
return 0;
}
/* my output
i = 42, j = -1 | k = 9 | f = 1.23, g = 4.56 | p = 0012FF88, q = 0012FF84
i = -1, j = 42 | k = 9 | f = 4.56, g = 1.23 | p = 0012FF84, q = 0012FF88
*/
Thanks for ur solution! :)
Nice, but can you spot the bug in Dave's solution?
There's a hint in the clc FAQ ;)
Thanks for the tip :)
You could avoid the entire temporary variable with the XOR method:
Code:#include <stdio.h>
#define swap(a,b) a^=b^=a^=b
int main(void)
{
int i, j;
i = 5;
j = 10;
printf("i: %d, j: %d\n", i, j);
swap(i, j);
printf("i: %d, j: %d", i, j);
return 0;
}
Yeah, that too (or why it's a bad idea) is also in the clc FAQ
http://www.eskimo.com/~scs/C-faq/faq.html
why does everyone uses thestuff ???Code:do{...}while(0);
two simple brackets do just fine {...}
That fails when a and b are equal.Quote:
Originally Posted by SirNot
My way, unfortunatly C99 or C++
good stuff about this is that it works for any type, including unions, structs or even C++ classes.Code:#define swap(a,b) {\
char t[sizeof(a)];\
memcpy(t,&a,sizeof(a));\
memcpy(&a,&b,sizeof(a));\
memcpy(&b,t,sizeof(a));\
}
I believe it's stated in the standard somewhere that you should use that. And actually, it's:Quote:
Originally Posted by xErath
That way when someone using the macro uses it, they can tack on a semicolon at the end and be correct.Code:do{...}while(0)
>Nice, but can you spot the bug in Dave's solution?
:o
>why does everyone uses the
>two simple brackets do just fine {...}
Or, follow the link.Code:#include <stdio.h>
#define why() { puts("because"); }
int main(void)
{
int i = 42;
if ( i )
why();
else
puts("oh");
return 0;
}
>That fails when a and b are equal.
It UB, pure and simple.
Going back to the OP's original question, (which I'm still not too clear about) GCC provides an operator called typeof() - would this help? I appreciate the non-portability implications.
>>GCC provides an operator called typeof()
It does? I could't find anything on it.
Even if that operator exists, I wouldn't suggest using it. If you can't figure out a solution to your problem that works in most compilers, then you need to put your thinking cap back on.
>Even if that operator exists, I wouldn't suggest using it. If you can't figure out a solution to your problem that works in most compilers, then you need to put your thinking cap back on.
I do agree. It looks like a nice feature, but I would never use it personally. A quick search on google shows that MS Visual C++ supports it too. Maybe one day it'll become part of the standard, then we can all use it.