c语言用函数指针交换变量值,指针学习:指针变量作为函数参数实现交换两个整数...

#include 

void swap (int *p, int *p2); /*propotypes declaration*/

void main()

{

int a1, a2, *ptr1, *ptr2;

printf("Enter two integer(separate by commas):\n");

scanf("%d,%d",&a1,&a2); /*enter a1 and a2 value*/

ptr1 = &a1;    /*ptr1 point to a1*/

ptr2 = &a2;    /*ptr2 point to a2*/

printf("Before change the address:&a1 = %p, a2 = %p\n",&a1,&a2);  /*print the address of a1 and a2*/

printf("Before change the addrees:ptr1 = %p, ptr2 = %p\n\n",ptr1,ptr2);/*print the value of ptr1 and ptr2*/

swap(ptr1, ptr2);  /*call function, use pointer variable as paraments*/

printf("After change value:a1 = %d, a2 = %d\n",a1,a2);     /*print the value of a1 and a2*/

printf("After change value:*ptr1 = %d, *ptr2 = %d\n",*ptr1,*ptr2);  /*print the value which ptr1 and ptr2 point to*/

printf("\nAfter change the address:&a1 = %p, &a2 = %p\n",&a1,&a2);   /*print a1 and a2's address*/

printf("After change the address:ptr1 = %p, ptr2 = %p\n",ptr1,ptr2);/*print ptr1 and ptr2's value*/

}

void swap(int *p1, int *p2)   /*define function*/

{

int t;

/*----------------change the value which p1 and p2 point's to----------------*/

t = *p1;

*p1 = *p2;

*p2 = t;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值