下面是从函数角度,还有简单的交换 法去实现两个数的交换。其中函数用到指针,通过前两种方法可以深刻的体会到指针变得的含义。
#include <stdio.h>
void swap(int *a,int *b){
int temp;
temp=*a;
*a=*b;
*b=temp;
}
void main()
{
int x=2, y=5;
int *m,*p;
m=&x;
p=&y;
swap(m,p);
printf("%d,%d",x,y);
}
#include <stdio.h>
void main()
{
int a,b,temp;
a=2;b=5;
temp=a;
a=b;
b=temp;
printf("%d,%d",a,b);
}
#include <stdio.h>
void swap(int *a,int *b)
{
int *temp;
temp=a;
a=b;
b=temp;
printf("%d,%d",*a,*b);
}
void main()
{
int x=2, y=5;
int *m,*p;
m=&x;
p=&y;
swap(m,p);
}