|
发表于 2016-11-29 20:43:09
|
显示全部楼层
本楼为最佳答案
C语言指针实现
- #include <stdio.h>
- void fun(void *num,int type);
- int main()
- {
- int a= 5;
- long b = 4;
- float c = 9;
- double d = 15;
- fun(&a,0);
- fun(&b,1);
- fun(&c,2);
- fun(&d,3);
- printf("%d\n",a);
- printf("%ld\n",b);
- printf("%f\n",c);
- printf("%lf\n",d);
- return 0;
- }
- void fun(void *num, int type)
- {
- int *p0;
- long *p1;
- float *p2;
- double *p3;
- switch(type)
- {
- case 0:
- p0 = (int *)num;
- *p0 /= 2;
- break;
- case 1:
- p1 = (long *)num;
- *p1 /= 2;
- break;
- case 2:
- p2 = (float *)num;
- *p2 /= 2;
- break;
- case 3:
- p3 = (double *)num;
- *p3 /= 2;
- break;
- default:
- printf("type incorrect!\n");
- }
- }
复制代码 |
|