#include <iostream>
#include <string.h>
#include <stdlib.h>
using namespace std;
void swapTest(void *swapA,void *swapB,int size)
{
char *buff=(char *)malloc(size);
memcpy(buff,swapA,size);
memcpy(swapA,swapB,size);
memcpy(swapB,buff,size);
}
int main()
{
double x=0.123;
double y=0.456;
cout<<"x="<<x<<","<<"y="<<y<<endl;
swapTest((void *)&x,(void *)&y,sizeof(double));
cout<<"after swap"<<endl;
cout<<"x="<<x<<","<<"y="<<y<<endl;
cout<<"this is test"<<endl;
}
上述变量x,y可以定义为char,int ,float,double
时间: 2024-11-10 05:41:10