用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;
}
#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
- 用Void指针的交换
- void指针(void *的用法)
- void指针(void *的用法)
- void指针(void *的用法)
- void指针(void *的用法)
- void指针(void *的用法)
- void指针(void *的用法)
- void指针(void *的用法)
- 指向void 的指针
- void* 指针的二三事
- void指针的操作
- void指针的使用
- 指向void的指针
- void指针的用法
- void指针的作用
- void指针的使用
- void 指针的转换
- void指针的使用
- Linux内核分析(八)——进程的调度
- hdoj 1420 Prepared for New Acmer
- hdu 1204 糖果大战 (Markov Chains求期望)
- 局部常量字符串指针及常量字符串数组 返回值
- HDU 1598--find the most comfortable road【并查集 + 枚举】
- 用Void指针的交换
- socket
- cocos2dx+lua注册事件函数详解
- PHP 过滤HTML中除了img标签外其它所有标签
- Java 面向对象之static关键字
- 15/4/24
- Android之Fragment
- 安卓学习第十天:继承,多继承,复写,转型,super
- 《博弈论》之囚犯困境与破解之道