Input two integers, A and B, and output A and B in the order of first largest and then smallest. The code is as follows: How to reference a variable address & take address operator *Pointer operator (or indirect access operator) #include "stdafx.h" #include <stdio.h> int main(int argc, char* argv[])
{ int *p1,*p2,*p,a,b; printf("please enter two integer numbers:"); scanf("%d,%d",&a,&b); p1=&a; p2=&b; if(a<b) {p1=&b;p2=&a;}//Pointer emphasis {p=p1; p1=p2; p2=p; Now it is directly assigned new values to P1 and P2, so that there is no need to define the intermediate variable p, and the program can become more concise This algorithm does not swap the values of integer variables, but the values of two pointers, (addresses of a and b)
printf("a=%d,b=%d\n",a,b); printf("max=%d,min=%d\n",*p1,*p2); return 0;
}
|