用C交换指针(CHAR,INT) [英] Swapping pointers in C (char, int)

查看:99
本文介绍了用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;
}

不过,如果我想交换两个字符指针,我需要做的是这样

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;
}

因为如果我这样做

because if I do

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

编译器抱怨前任pression * A = * B,因为它无法改变的值。
如果我想交换两个字符串(即的char * S1 =你好;字符* S2 =再见; )如何总会有做呢?

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!

请注意如何在函数中我们没有分配给指针,但分配给它们指向的东西。而指针指向我们的变量 X 。该功能直接改变存储中的值的我们的通过,我们给它的指针变量。而这正是我们所需要的。

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 正在改变我们的变量指向,而不是指针的值!我们给功能的手段来引用变量 X ,但我们希望他们参考 XP YP

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 现在指向),而不是价值,他们指向。我们给了它一个方式是指我们的指针变量,所以它可以改变他们!

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天全站免登陆