在 C 中交换指针(char,int) [英] Swapping pointers in C (char, int)

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

问题描述

我一直在努力理解在 C 中交换指针时的不同行为.如果我想交换两个 int 指针,那么我可以做

I have been struggling to understand the different behaviour when swapping pointers in C. If I want to swap two int pointers, then I can do

void intSwap (int *pa, int *pb){
    int temp = *pa;
    *pa = *pb;
    *pb = temp;
}

但是,如果我想交换两个 char 指针,我需要做类似的事情

However, if I want to swap two char pointers I need to do something like

void charSwap(char** a, char** b){
    char *temp = *a;
    *a = *b;
    *b = temp;
}

因为如果我这样做

void charSwap(char* a, char* b){
    char temp = *a;
    *a = *b;
    *b = temp;
}

编译器抱怨表达式 *a = *b 因为它不能改变值.如果我想交换两个 strings(即 char* s1="Hello"; char* s2="Bye"; )怎么做?

the compiler complains about the expression *a = *b as it cannot change the values. If I want to swap two strings (i.e. char* s1= "Hello"; char* s2="Bye"; )how would one do it?

你能给我一点帮助吗?我想真正了解它是如何工作的,这样我就不需要一直反复试验,直到我得到正确的答案.我希望它对其他人有用.

Could you please give me a little bit of help? I would like to really learn how it works so I will not need to experience trial and error all the time until I get the right answer. I hope it's useful for many other people.

推荐答案

您首先需要了解的是,当您将某些内容传递给函数时,会将某些内容复制到函数的参数中.

The first thing you need to understand is that when you pass something to a function, that something is copied to the function's arguments.

假设您有以下内容:

void swap1(int a, int b) {
    int temp = a;
    a = b;
    b = temp;
    assert(a == 17);
    assert(b == 42);
    // they're swapped!
}

int x = 42;
int y = 17;
swap1(x, y);
assert(x == 42);
assert(y == 17);
// no, they're not swapped!

原始变量不会被交换,因为它们的值被复制到函数的参数中.然后该函数继续交换这些参数的值,然后返回.原始值不会改变,因为函数只交换自己的私有副本.

The original variables will not be swapped, because their values are copied into the function's arguments. The function then proceeds to swap the values of those arguments, and then returns. The original values are not changed, because the function only swaps its own private copies.

现在我们如何解决这个问题?该函数需要一种方法来引用原始变量,而不是它们值的副本.我们如何在 C 中引用其他变量?使用指针.

Now how do we work around this? The function needs a way to refer to the original variables, not copies of their values. How can we refer to other variables in C? Using pointers.

如果我们将指向变量的指针传递给函数,函数可以交换我们变量中的值,而不是它自己的参数副本.

If we pass pointers to our variables into the function, the function can swap the values in our variables, instead of its own argument copies.

void swap2(int* a, int* b) {
    int temp = *a;
    *a = *b;
    *b = temp;
    assert(*a == 17);
    assert(*b == 42);
    // they're swapped!
}

int x = 42;
int y = 17;
swap2(&x, &y); // give the function pointers to our variables
assert(x == 17);
assert(y == 42);
// yes, they're swapped!

注意在函数内部我们没有分配给指针,而是分配给它们指向的东西.指针指向我们的变量 xy.该函数通过我们给它的指针直接改变存储在我们变量中的值.而这正是我们所需要的.

Notice how inside the function we're not assigning to the pointers, but assigning to what they point to. And the pointers point to our variables x and y. The function is changing directly the values stored in our variables through the pointers we give it. And that's exactly what we needed.

现在如果我们有两个指针变量并且想要交换指针本身(而不是它们指向的值)会发生什么?如果我们传递指针,指针将被简单地复制(而不是它们指向的值)到参数.

Now what happens if we have two pointer variables and want to swap the pointers themselves (as opposed to the values they point to)? If we pass pointers, the pointers will simply be copied (not the values they point to) to the arguments.

void swap3(int* a, int* b) {
    int* temp = a;
    a = b;
    b = temp;
    assert(*a == 17);
    assert(*b == 42);
    // they're swapped!
}
void swap4(int* a, int* b) {
    int temp = *a;
    *a = *b;
    *b = temp;
    assert(*a == 17);
    assert(*b == 42);
    // they're swapped!
}

int x = 42;
int y = 17;
int* xp = &x;
int* yp = &y;
swap3(xp, yp);
assert(xp == &x);
assert(yp == &y);
assert(x == 42);
assert(y == 17);
// Didn't swap anything!
swap4(xp, yp);
assert(xp == &x);
assert(yp == &y);
assert(x == 17);
assert(y == 42);
// Swapped the stored values instead!

函数 swap3 只交换它自己在参数中获取的指针的私有副本.这与我们在使用 swap1 时遇到的问题相同.而 swap4 正在改变我们的变量指向的值,而不是指针!我们给函数提供了一种引用变量 xy 的方法,但我们希望它们引用 xpyp.

The function swap3 only swaps its own private copies of our pointers that it gets in its arguments. It's the same issue we had with swap1. And swap4 is changing the values our variables point to, not the pointers! We're giving the function a means to refer to the variables x and y but we want them to refer to xp and yp.

我们如何做到这一点?我们把他们的地址传给它!

How do we do that? We pass it their addresses!

void swap5(int** a, int** b) {
    int* temp = *a;
    *a = *b;
    *b = temp;
    assert(**a == 17);
    assert(**b == 42);
    // they're swapped!
}


int x = 42;
int y = 17;
int* xp = &x;
int* yp = &y;
swap5(&xp, &yp);
assert(xp == &y);
assert(yp == &x);
assert(x == 42);
assert(y == 17);
// swapped only the pointers variables

通过这种方式,它交换了我们的指针变量(注意 xp 现在如何指向 y)而不是它们指向的值.我们给了它一种引用我们的指针变量的方法,所以它可以改变它们!

This way it swaps our pointer variables (notice how xp now points to y) but not the values they point to. We gave it a way to refer to our pointer variables, so it can change them!

现在应该很容易理解如何以 char* 变量的形式交换两个字符串.交换函数需要接收指向 char* 的指针.

By now it should be easy to understand how to swap two strings in the form of char* variables. The swap function needs to receive pointers to char*.

void swapStrings(char** a, char** b){
    char *temp = *a;
    *a = *b;
    *b = temp;
    assert(strcmp(*a, "world") == 0);
    assert(strcmp(*b, "Hello") == 0);
}

char* x = "Hello";
char* y = "world";
swapStrings(&x, &y);
assert(strcmp(x, "world") == 0);
assert(strcmp(y, "Hello") == 0);

这篇关于在 C 中交换指针(char,int)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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