C语言 简单的交换功能……为什么这个不交换?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3835922/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 06:33:54  来源:igfitidea点击:

Simple swap function...why doesn't this one swap?

cpointers

提问by Adam Soffer

I'm new to C and still trying to grasp the concept of pointers. I know how to write a swap function that works...I'm more concerned as to why this particular one doesn't.

我是 C 新手,仍然试图掌握指针的概念。我知道如何编写一个有效的交换函数......我更关心为什么这个特定的函数没有。

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

int main()
{
 int x = 5, y = 10;
 int *a = &x, *b = &y;
 swap(a, b);
 printf(“%d %d\n”), *a, *b);
}

回答by zildjohn01

You're missing *s in the swap function. Try:

*在交换函数中缺少s。尝试:

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

That way, instead of just swapping the pointers, you're swapping the ints that the pointers are pointing to.

通过这种方式,而不是只交换指针,你调换int的指针指向小号

回答by caf

Your swap()function doeswork, after a fashion - it swaps the values of the variables aand bthat are local to swap(). Unfortunately, those are distinct from the aand bin main()- so you don't actually see any effect from swapping them.

您的swap()函数确实可以工作,但它会交换变量的值,a并且b这些值是swap(). 不幸的是,这些abin和in不同,main()因此您实际上看不到交换它们的任何效果。

回答by Ziffusion

When thinking about pointers, you need to be clear on a few abstractions.

在考虑指针时,您需要清楚一些抽象。

An object in memory. This can be of any type (and size). An integer object, for example, will occupy 4 bytes in memory (on 32 bit machines). A pointer object will occupy 4 bytes in memory (on 32 bit machines). As should be obvious, the integer object holds integer values; a pointer object holds addresses of other objects.

内存中的一个对象。这可以是任何类型(和大小)。例如,整数对象将占用 4 个字节的内存(在 32 位机器上)。一个指针对象将在内存中占用 4 个字节(在 32 位机器上)。很明显,整数对象保存整数值;指针对象保存其他对象的地址。

The C programming language lets symbols (variables) represent these objects in memory. When you declare,

C 编程语言让符号(变量)代表内存中的这些对象。当你声明时,

int i;

国际我;

the symbol (variable) i represents some integer object in memory. More specifically, it represents the value of this object. You can manipulate this value by using i in the program.

符号(变量) i 表示内存中的某个整数对象。更具体地说,它代表了这个对象的价值。您可以通过在程序中使用 i 来操作此值。

&i will give you the address of this object in memory.

&i 会给你这个对象在内存中的地址。

A pointer object can hold the address of another object. You declare a pointer object by using the syntax,

一个指针对象可以保存另一个对象的地址。您可以使用语法声明一个指针对象,

int* ptr;

int* ptr;

Just like other variables, the pointer variable represents the value of an object, a pointer object. This value just happens to be an address of some other object. You set the value of a pointer object like so,

就像其他变量一样,指针变量代表一个对象的值,一个指针对象。这个值恰好是某个其他对象的地址。你像这样设置指针对象的值,

ptr = &i;

ptr = &i;

Now, when you say ptr in the program, you are referring to its value, which is the address of i. But if you say *ptr, you are referring to not the value of ptr, but rather the value of the object whose address is in ptr i.e. i.

现在,当您在程序中说 ptr 时,您指的是它的值,即 i 的地址。但是如果你说*ptr,你指的不是ptr的值,而是地址在ptr iei中的对象的值

The problem with your swap function is that you are swapping values of pointers, not the values of objects that these pointers hold addresses for. To get to the values of objects, you would have to use *ptr.

交换函数的问题在于您交换的是指针的值,而不是这些指针保存地址的对象的值。要获取对象的值,您必须使用 *ptr。

回答by Carl Norum

C is a pass-by-value language. Your swaproutine doesn't dereference the pointers passed to it, so from main's perspective nothing has happened.

C 是一种传值语言。您的swap例程不会取消引用传递给它的指针,因此从main的角度来看什么也没发生。

回答by shuriquen

Umm maybe using this

嗯也许使用这个

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

int main()
{
 int x = 5, y = 10;
 int *a = &x, *b = &y;
 swap(&a, &b);
 printf(“%d %d\n”), *a, *b);
}

回答by Hitesh Modha

Without using a third variable (temp)

不使用第三个变量(温度)

void swap(int* a,int* b)
{ 
 // a = 10, b = 5;
  *a = *a + *b;  // a now becomes 15
  *b = *a - *b;  // b becomes 10
  *a = *a - *b;  // a becomes 5
}

回答by Preet Sangha

The pointers are passed by value. This means a & b are still a and b when the come back from the function;

指针按值传递。这意味着当函数返回时 a & b 仍然是 a 和 b;

try something like this

尝试这样的事情

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

回答by abelenky

The right way to do it:

正确的做法:

void swap(int* a, int* b)
{
    int temp = *a;  // Temp is set to the value stored at a (5)
    *a = *b;        // value stored at a is changed to the value stored at b (10)
    *b = temp;      // value stored in address b is changed to 5. 
}

回答by AnT

It does swap. It swaps local pointers aand binside swapfunction. It swaps them perfectly fine, as it should.

它确实交换。它交换本地指针ab内部swap函数。它完全可以很好地交换它们,这是应该的。

If you want to swap the values these pointers are pointing to, you should re-implement your swapfunction accordingly, i.e. make it swap the pointed values, not the pointers.

如果你想交换这些指针指向的值,你应该相应地重新实现你的swap函数,即让它交换指向的值,而不是指针。

回答by Sridhar Iyer

zildjohn1's answer is the easiest and clearest way to do it. However if you insist on swapping the pointers, then you have to pass the pointer to the pointer because the pointer itself is passed by value.

zildjohn1 的答案是最简单、最清晰的方法。但是,如果您坚持交换指针,则必须将指针传递给指针,因为指针本身是按值传递的。