1. 程式人生 > >c語言,通過指標交換兩個數的值

c語言,通過指標交換兩個數的值

#include<stdio.h>

void swap(int *p ,int *p1){
	int *temp ;
	temp = p;
	p= p1;
	p1 = temp;
	
}
void swap2(int *p ,int *p1){
	int temp ;
	temp = *p;
	*p= *p1;
	*p1 = temp;
	
}
void main(){
	int a = 1;
	int b = 2;
	int *p =&a;
	int *p1 = &b;
	swap(p,p1);
	printf("%d,%d",*p,*p1);//結果為  1,2
	swap2(p,p1);
	printf("%d,%d",*p,*p1);//結果為  2,1

}