指针混乱:C语言中的交换方法 [英] Pointer Confusion: swap method in c

查看:186
本文介绍了指针混乱:C语言中的交换方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include<stdio.h>
void swap(int *a,int *b){
    int p=*b;
    *b=*a;
    *a=p;

    /*int *p=b;
    b=a;
    a=p;
    */
}

int main(){
    int a,b;
    scanf("%d %d",&a,&b);
    swap(&a,&b);
    printf("%d %d",a,b);
}

上面是代码.

如果我将3 5作为输入,那么它应该交换其值,而5 3应该作为输出出来. 我通过尝试int p = * b事情得到了答案

If I put 3 5 as an input, then it should swap its values, and 5 3 should come out as an output. I got my answer by trying int p=*b thing

但是我也尝试评论了一部分,但是没有用.

However I also tried commented part, but it didn't work.

所以,我在swap和main中检查了他们的地址.

So, I checked their address in swap and in main.

在swap int * a和int * b中,其地址已更改

In swap int *a and int *b their address changed

但是,当我回到main时,a和b的地址没有改变...

However, when I came back to main, a and b 's addresses were not changed...

所以首先我想:因为参数int * a,int * b是局部变量,它在main中是否没有改变?

So first I thought: is it not changed in main because parameter int *a,int *b is local variable?

但是我也了解到,当使用指针和参数作为参数时,它们的值可以改变,与其他变量不同...

But I also learned that when pointers and arguments are used as arguments their value can change unlike other variables...

我真的很奇怪为什么第二个方法(注释部分)没有交换值...

I really wonder why the second method(commented part) is not swapping the values...

推荐答案

如果要在函数中更改原始对象,则必须通过引用将它们传递给函数.

If you want to change in a function original objects you have to pass them to the function by reference.

在C中,通过引用传递对象意味着通过指向原始对象的指针间接传递它们.

In C passing objects by reference means passing them indirectly through pointers that point to the original object.

否则,如果将原始对象本身传递给函数,则该函数将处理对象的副本.显然,更改副本不会影响原始对象.

Otherwise if you will pass the original objects themselves to the function the function will deal with copies of the objects. It is evident that changing copies does not influence on the original objects.

这正是此功能发生的事情

It is exactly what happens in this function

void swap(int *a,int *b){
    int *p=b;
    b=a;
    a=p;
}

该函数处理在此调用中作为参数传递给该函数的指针的副本

The function deals with copies of pointers passed to the function as argument in this call

swap(&a,&b);

该函数确实交换了两个声明为其参数的指针的值.但是它们不是传递给该函数的原始指针.它们是指针的副本.因此原始指针的值没有改变

That is the function indeed swapped values of the two pointers that are declared as its parameters. But they are not the original pointers passed to the function. They are copies of the pointers. So the values of the original pointers were not changed

函数交换通常可以通过以下方式查看

The function swap in general can look the following way

void swap( T *a, T *b )
{
    T tmp = *a;
    *a = *b;
    *b = tmp;
}  

其中T是相同类型说明符.

where T is same type specifier.

因此,如果您要交换类型为int的对象,则在上面的函数中T将为int并且该函数将看起来像

So if you want to swap objects of the type int then in the above function T will be int and the function will look like

void swap( int *a, int *b )
{
    int tmp = *a;
    *a = *b;
    *b = tmp;
}  

如果要交换类型为int *的指针的值,则T将为int *,该函数将类似于

If you want to swap values of pointers of the type int * then T will be int * and the function will look like

void swap( int **a, int **b )
{
    int *tmp = *a;
    *a = *b;
    *b = tmp;
}  

这是一个演示程序.

#include <stdio.h>

void swap1( int *pa, int *pb )
{
    int tmp = *pa;
    *pa = *pb;
    *pb = tmp;
}

void swap2( int **ppa, int **ppb )
{
    int *tmp = *ppa;
    *ppa = *ppb;
    *ppb = tmp;
}

int main(void) 
{
    int a = 3, b = 5;

    swap1( &a, &b );

    printf( "a = %d b = %d\n", a, b );

    //  reset again the values of the variables
    a = 3; b = 5;

    int *pa = &a, *pb = &b;

    swap2( &pa, &pb );

    printf( "*pa = %d *pb = %d\n", *pa, *pb );

    return 0;
}

其输出为

a = 5 b = 3
*pa = 5 *pb = 3

也就是说,首先在程序中交换了两个类型为int的对象,所以想象的类型说明符Tint.

That is at first in the program two objects of the type int are swapped, So the imagined type specifier T is int.

然后交换两个指向对象ab的指针.因此,想象中的类型说明符T int *.

Then two pointers that point to the objects a and b are swapped. So the imagined type specifier T int *.

交换指针后,指针pa现在指向对象b,指针pb现在指向对象a.

After swapping the pointers the pointer pa now points to the object b and the pointer pb now points to the object a.

这篇关于指针混乱:C语言中的交换方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆