指针真的很强大!
calculator函数是void型的,但是经过一系列操作却可以在一个函数里面进行加、减、乘、除的操作,不得不说,跟单个运算写一个函数想比,代码简化了不少!- #include <stdio.h>
- void caculator(int x,int y,int *add,int *jian,int *cheng,float *div);
- int main(int argc, const char * argv[]) {
- int add=0,jian=0,cheng=0;
- float div=0;
- caculator(14, 2, &add, &jian, &cheng, &div);
- printf("add=%d\n",add);
- printf("jian=%d\n",jian);
- printf("cheng=%d\n",cheng);
- printf("div=%.2f\n",div);
- return 0;
- }
- void caculator(int x,int y,int *add,int *jian,int *cheng,float *div)
- {
- *add = x+y;
- *jian = x-y;
- *cheng = x*y;
- *div = x/(float)y;
- }
复制代码 |
|