指针例题

例.10.1

#include<stdio.h>
void main()
{

   int a,b;
   int*pointer_1,*pointer_2;
   a=100;b=10;
   pointer_1=&a;/*把变量a的地址赋给pointer_1*/
   pointer_2=&b;/*把变量b的地址赋给pointer_2*/
   printf("%d,%d\n",a,b);
    printf("%d,%d\n",*pointer_1,*poniter_2);
}

例10.2

#include<stdio.h>
void main()
{
 int *p1,*p2,*p,a,b;
 scanf("%d,%d",&a,&b);
 p1=&a;p2=&b;
 if(a<b)
 {p1=&b;p2=&a;}
 printf("a=%d,b=%d\n\n",a,b);
 printf("max=%d,min=%d\n",*p1,*p2);
}

5,9 a=5,b=30

max=30,min=5

-------------------------------- Process exited after 7.975 seconds with return value 13 请按任意键继续. . .

例10.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);
}
void swap(int*p1,int*p2)
{int temp;
temp=*p1;
*p1=*p2;
*p2=temp;
 }

5,9

30,5

-------------------------------- Process exited after 4.355 seconds with return value 6 请按任意键继续. . .

例10.4

#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);
}
void exchange(int *q1,int *q2,int *q3)
{void swap(int *pt1,int *pt2);
if(*q1<*q2)swap(q1,q2);
if(*q1<*q3)swap(q1,q3);
if(*q2<*q3)swap(q2,q3);
}
void swap(int *pt1,int *pt2)
{int temp;
temp=*pt1;
*pt1=*pt2;
*pt2=temp;
}

9,0,10

9,0,0

-------------------------------- Process exited after 4.179 seconds with return value 7 请按任意键继续. . .

 例10.5

 

posted @ 2017-03-26 11:19  伊靖雯  阅读(292)  评论(1编辑  收藏  举报