指针的用法 2022_4_27
made by :jy
示例作用:实现值的交换
示例1:
实参传入地址:&a和&b;
形参接指针:p1和p2;
#include<stdio.h>
void exchange(int *p1,int *p2){
int p3 = *p1;
*p1 = *p2;
*p2 = p3;
};
void main(){
int a = 1;
int b = 2;
exchange(&a,&b);
printf("a = %d,b = %d\n",a,b);
}
也可以写成:
#include<stdio.h>
void exchange(int *d1,int *d2){
int d3 = *d1;
*d1 = *d2;
*d2 = d3;
};
void main(){
int a = 1;
int b = 2;
int *p1 = &a;
int *p2 = &b;
exchange(p1,p2);
printf("a = %d,b = %d\n",a,b);
}