题解 | 编写函数实现两数交换(指针方式)

编写函数实现两数交换(指针方式)

https://www.nowcoder.com/practice/380c764bdef64ad696cdfa90bfe85156

这个要记住

函数体内的形式参数得是指针变量,实参为变量地址,在函数体内,得用数据变量来进行交换,因为我们是利用地址储存的数据变量值来交换。

#include <stdio.h>

// write your code here......
void swap(int *p,int *q){
    int temp;
    temp=*q;
    *q=*p;
    *p=temp;
}


int main() {

    int m, n;
    scanf("%d",&m);
    scanf("%d",&n);

    // write your code here......
    int *p=&m;
    int *q=&n;
    swap(&m,&n);
    printf("%d %d",m,n);

    return 0;
}
全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务