您的位置:首页 > 其它

使用void指针给函数传递不同的数据类型

2015-10-25 00:00 513 查看
#include <stdio.h>
void half(void *pval, char type);

int main(void){
int i = 20;
long l = 100000;
float f = 12.456;
double d = 123.044444;

printf("\n%d", i);
printf("\n%ld", l);
printf("\n%f", f);
printf("\n%lf\n\n", d);

half(&i, 'i');
half(&l, 'l');
half(&d, 'd');
half(&f, 'f');

printf("\n%d", i);
printf("\n%ld", l);
printf("\n%f", f);
printf("\n%lf\n", d);
return (0);
}

void half(void *pval, char type){
switch(type){
case 'i':{
*((int *)pval) /= 2;
break;
}
case 'l':{
*((long *)pval) /=2;
break;
}
case 'f':{
*((float *)pval) /=2;
break;
}
case 'd':{
*((double *)pval) /=2;
break;
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: