构建comp函数
#include<stdio.h> int *comp(int *x,int *y) { if(*x>*y)return x; else return y; } int main() { int a,b,c; int *x; int *y; a=5; b=7; x=&a; y=&b; c=*comp(x,y); printf("%d\n",c); }
使用自定义函数来使程序更简化,需要变更其他条件时直接修改自定义函数即可达成
7 -------------------------------- Process exited after 2.197 seconds with return value 0 请按任意键继续. . .
2.因为int *m是一个指针,并且在定义之后未初始化,所以其指向的地址也是随机的,需要一个临时变量来令其指向才能交换。
3.被调用函数没有返回值,而交换时只是交换q,p地址。
#include<stdio.h> void swap(int *a,int *b) { int t; t=*a; *a=*b; *b=t; } main() { int x,y; scanf("%d,%d",&x,&y); swap(&x,&y); printf("%d,%d",x,y); }
3,2 2,3 -------------------------------- Process exited after 5.315 seconds with return value 0 请按任意键继续. . .