清华大学出版社 TSINGHUA UNIVERSITY PRESS p1 p a a 8.b p p2 P2 8.b
清华大学出版社 TSINGHUA UNIVERSITY PRESS 8.2.3指针变量作为函数参数 例8.3对输入的两个整数按大小顺序输出 #include <stdio. h> void main() void swap (int *p 1, int*p 2) int a, b pointer 1 ointer 2 scanf(Io d,%d",&a,&b) pointer 1 =& a; pointer 2=& b; if (a b) swap( pointer 1, pointer 2) printf ("\n %d, %d\n",a,b);
8.2.3 指针变量作为函数参数 例8 . 3 对输入的两个整数按大小顺序输出 #include <stdio.h> void main() {void swap(int *p1,int *p2); int a,b; int *pointer_1,*pointer_2; scanf(″%d,%d″,&a,&b); pointer_1 =&a; pointer_2 =&b; if(a<b) swap( pointer_1 , pointer_2 ); printf(″\n%d,%d\n″,a,b); }
清华大学出版社 TSINGHUA UNIVERSITY PRESS void swap (int *p 1, int *p 2) I int temp temp=*p 1: p2; *p 2=temp;
void swap(int *p1,int *p2) { int temp; temp=*p1; *p1=*p2; *p2=temp; }
清华大学出版社 TSINGHUA UNIVERSITY PRESS pI a 8 a pointer a pointer 1 pointer 1 pointer_ 1 a 8 pointer_2 p2 p2 pointer 2 b 8.b 8.b & b pointer_ 2 pointer 2 8.b 8.b
清华大学出版社 TSINGHUA UNIVERSITY PRESS 例8.4输入3个整数a,b,c,要求按大小顺序将它们输出。 用函数实现改变这3个变量的值。 #include <stdio. h> VO id main t void exchange (int* q 1, int q 2, int*q3) int a, b, c, *p1, *p2, *p3; scanf("%d, % d, d", &a&b &c) p1=&a;p2=&b;p3=&c; exchange( p 1 p3) printf ("\n%d, d, %d\ n a C
例8.4输入3个整数a,b,c,要求按大小顺序将它们输出。 用函数实现改变这3个变量的值。 #include <stdio.h> void main() { void exchange(int *q1, int *q2, int *q3); int a,b,c,*p1,*p2,*p3; scanf(″%d,%d,%d″ ,&a, &b, &c); p1=&a;p2=&b;p3=&c; exchange (p1,p2,p3); printf(″\n%d,%d,%d\n″,a,b,c); }