69,396
社区成员
发帖
与我相关
我的任务
分享
#ifndef _SWAP_H
#define _SWAP_H
#define swap(x, y) \
{ \
if (sizeof(x) == sizeof(int)) { \
int *_p1 = (int *) &x; \
int *_p2 = (int *) &y; \
*_p1 ^= *_p2; \
*_p2 ^= *_p1; \
*_p1 ^= *_p2; \
} else { \
long long *_p1 = (long long *) &x; \
long long *_p2 = (long long *) &y; \
*_p1 ^= *_p2; \
*_p2 ^= *_p1; \
*_p1 ^= *_p2; \
} \
}
#endif
#include "swap.h"
#include <stdio.h>
int
main(void)
{
int i = 3;
int j = 4;
printf("i = %d, j = %d\n", i, j);
swap(i, j);
printf("i = %d, j = %d\n", i, j);
float pi = (float) 3.14159;
float e = (float) 2.71828;
printf("pi = %f, e = %f\n", pi, e);
swap(pi, e);
printf("pi = %f, e = %f\n", pi, e);
double g = 6.67259;
double c = 2.9979;
printf("g = %lf, c = %lf\n", g, c);
swap(g, c);
printf("g = %lf, c = %lf\n", g, c);
return 0;
}
#include <assert.h>
#include <stdio.h>
#include <mem.h>
#define SWAP(a, b) \
do { \
char temp[sizeof(a)]; \
assert(sizeof(a) == sizeof(b)); \
MY_TINY_MEMCPY((void*)temp, &(a), sizeof(a)); \
(a) = (b); \
(b) = (a); \
MY_TINY_MEMCPY(&(b), (void*)temp, sizeof(a)); \
} while(0)
#define MY_TINY_MEMCPY(dest, src, len) \
((len) == sizeof(char) ? (void)(*(char*)(dest) = *(char*)(src)) : \
(len) == sizeof(short) ? (void)(*(short*)(dest) = *(short*)(src)) : \
(len) == sizeof(int) ? (void)(*(int*)(dest) = *(int*)(src)) : \
(len) == sizeof(long) ? (void)(*(long*)(dest) = *(long*)(src)) : \
(len) == sizeof(float) ? (void)(*(float*)(dest) = *(float*)(src)) : \
(len) == sizeof(double) ? (void)(*(double*)(dest) = *(double*)(src)) : \
(void)memcpy((dest), (src), (len)))
int main()
{
int i_a = 1, i_b = 2;
long lval = 3;
float fval = 4.0f;
double dval = 5.0;
void* pval = (void*)0x1234;
struct { char name[10]; void* next; } node1 = {"abcd"}, node2 = {"efg"};
union { double d; void* p; } u1, u2;
SWAP(i_a, i_b);
SWAP(fval, dval);
SWAP(node1, node2);
SWAP(u1, u2);
SWAP(lval, pval); // error!
SWAP(lval, fval); // error!
return 0;
}
尽管不可谓不用心,但 LZ 能否看出代码里错漏百出啊。(a)^=(b)^=(a)^=(b)
#include <stdio.h>
#define SWAP(a,b) do ((&(a))!=(&(b)))?((a)^=(b)^=(a)^=(b)):((a)=(a)); while (0)
char *p1="1" ,*p2="2" ;
char c1=1 , c2=2 ;
short s1=1 , s2=2 ;
int i1=1 , i2=2 ;
__int64 I1=1i64, I2=2i64;
float f1=1.0f, f2=2.0f;
double d1=1.0 , d2=2.0 ;
void main() {
SWAP((int)p1,(int)p2); printf("char * %5s, %5s\n",p1,p2);
SWAP(c1,c2); printf("char %5d, %5d\n",c1,c2);
SWAP(s1,s2); printf("short %5d, %5d\n",s1,s2);
SWAP(i1,i2); printf("int %5d, %5d\n",i1,i2);
SWAP(I1,I2); printf("__int64 %5I64d,%5I64d\n",I1,I2);
SWAP(*(int *)&f1,*(int *)&f2);printf("float %5g, %5g\n",f1,f2);
SWAP(*(__int64 *)&d1,*(__int64 *)&d2);printf("double %5lg, %5lg\n",d1,d2);
SWAP(c1,c1);
printf("%d\n",c1);
}
//char * 2, 1
//char 2, 1
//short 2, 1
//int 2, 1
//__int64 2, 1
//float 2, 1
//double 2, 1
//2
#define _swap(x, y) \
do \
{ \
decltype(x) t = (x); \
(x) = (y); \
(y) = (t); \
} while (false);
struct st
{
int i;
float f;
short s;
};
int main()
{
int a = 0;
int b = 1;
_swap(a, b);
st s0;
s0.i = 0;
s0.f = 0.f;
s0.s = 0;
st s1;
s1.i = 1;
s1.f = 2.f;
s1.s = 3;
_swap(s0, s1);
return 0;
}