用Void指针的交换

来源:互联网 发布:js解密器 编辑:程序博客网 时间:2024/05/16 00:44

#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<string.h>
void swap(void *p,void *q,size_t size)
{
char *tmp = (char *)malloc(size);
assert(tmp != NULL);
memcpy(tmp,p,size);
memcpy(p,q,size);
memcpy(q,tmp,size);
free(tmp);
}
int main()
{
int x = 10;
int y = 20;
int *m = &x;
int *n = &y;
swap(m,n,2);
int arr[10] = {1,2,3,4,5,6,7,8,9,10};
int brr[10] = {11,22,33,44,55,66,77,88,99,100};
swap(arr,brr,sizeof(int[10]));
float a = 9.88f;
float b = 8.99f;
float *j = &a;
float *k = &b;
swap(j,k,2);
return 0;


}

0 0