#include<stdio.h>
#include<string.h>
#include<stdlib.h>
void swap(void* src,void* dest,int n)
{
char *tem=NULL;
tem=malloc(n);
memcpy(tem,src,n);
memcpy(src,dest,n);
memcpy(dest,tem,n);
free(tem);
}
int main(int argc,char **argv)
{
int a=3,b=4;
swap(&a,&b,sizeof(int));
printf("%d %d\n",a,b);
return 0;
}
编写一个函数,功能为互换任意两个相同类型变量的值
最新推荐文章于 2024-03-14 23:43:28 发布
本文介绍了一个使用C语言实现的通用内存交换函数,并通过一个简单的整数变量交换示例展示了其用法。该函数利用指针操作和memcpy函数来实现两个内存区域的内容互换。

3268

被折叠的 条评论
为什么被折叠?



