1. macros

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... Thanks; 2. Sounds like a homework assignment. Well, why don't you show us what you have so far? 3. 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. 4. 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. 5. Well, i just know to write a function like this...
Code:

#define isBigger(a,b) (((a) > (b))? (a):(b))
But i'm not sure how to do a swap function... 6. 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):
Code:
z = x
x = y
y = z
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. 7. i see, so is this correct?
Code:
#define swap(t, a, b) (t temp=a; a=b; b=temp;) 8. 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;) 9. 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? 10. 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:
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;
}
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. 11. Thanks for your help  12. 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
*/ 13. Thanks for ur solution!  14. Nice, but can you spot the bug in Dave's solution?
There's a hint in the clc FAQ  15. Thanks for the tip  Popular pages Recent additions 