Swapping addresses of pointers in C++

后端 未结 5 2067
温柔的废话
温柔的废话 2021-01-01 02:38

How can one swap pointer addresses within a function with a signature?

Let\'s say:

int weight, height;
void swap(int* a, int* b);

S

相关标签:
5条回答
  • 2021-01-01 02:59

    It seems you might be a bit confused about terms.

    An object usually has an address. That is the location in memory where the object is located. Some temporary objects don't have addresses, because they don't need to be stored. Such an exception is the temporary "4" object in the expression (2+2)*3

    A pointer is an object that stores an address of another object. Hence, for every type, there is a matching pointer. int has int*, std::string has std::string*, etcetera.

    Now, you write about "addresses of pointers". They exist. After all, I wrote that a pointer is an object, and thus it has its own address. And you can store that address in another pointer. For instance, you can store the address of and int* in an int* *. But do you really intended that? Or did you mean the "address referenced by a pointer"?

    Now, you give height and weight as examples. The standard way to swap them in C++ is simply std::swap(width, height). Note the std::, which is the prefix for C++ standard library functions. std::swap will swap almost everything. ints, floats, wives. (j/k).

    You have another swap function, apparently. It accepts two pointers to integers, which means it wants the addresses of two integers. Those are easy to provide, in this case. width is an integer, and &width is its address. That can be stored in the pointer argument int* a. Similarly, you can store the address &height in argument int*b. Putting it together, you get the call swap(&width, &height);

    How does this work? The swap(int*a, int*b) function has two pointers, holding the address of two integers in memory. So, what it can do is [1] set aside a copy of the first integer, [2] copy the second integer in the memory where the first integer was, and [3] copy the first integer back in the memory where the second integer was. In code:

     void swap(int *a, int *b)
     {
          int temp = *a; // 1
          *a = *b;       // 2
          *b = temp;     // 3
     }
    
    0 讨论(0)
  • 2021-01-01 02:59

    If you want to change address of pointers then you have to pass pointers of those pointers as your parameters:

    void swap(int **a, int **b); //as prototype
    

    Those examples are just changes values of pointers.

    0 讨论(0)
  • 2021-01-01 03:00

    If you want to swap the addresses that the pointers are pointing to, not just the values stored at that address, you'll need to pass the pointers by reference (or pointer to pointer).

    #include <cassert>
    void swap(int*& a, int*& b)
    {
        int* c = a;
        a = b;
        b = c;
    }
    
    int main()
    {
        int a, b;
        int* pa = &a;
        int* pb = &b;
    
        swap(pa, pb);
    
        assert(pa == &b);  //pa now stores the address of b
        assert(pb == &a);  //pb now stores the address of a
    }
    

    Or you can use the STL swap function and pass it the pointers.

    #include <algorithm>
    
    std::swap(pa, pb);
    

    Your question doesn't seem very clear, though.

    0 讨论(0)
  • 2021-01-01 03:01

    In C++ you would write

     void swap(int *a, int *b)
     {
        std::swap(*a, *b);
     }
    

    or rather just use:

     std::swap(a, b);
    
    0 讨论(0)
  • 2021-01-01 03:09

    The new answer (since the question has been reformulated)

    is that addressed of variables are determined at compile time and can therefore not be swapped. Pointers to variables however can be swapped.

    Old answer: this was the answer when the question still implied swapping the values of 2 variables by means of a function:

    function call:

     int width=10, height=20;
     swap(&width, &height)
    

    implementation:

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

    or...without using a temporary variable: ;^)

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

    do watch out that the last method breaks for the case: swap (&a, &a). Very sharply pointed out by user9876 in the comments.

    0 讨论(0)
提交回复
热议问题