1

I have a problem, I can't create a simple program for swapping two variables with a pointer. I tried in this way:

int main() {

int x=5, y=10;
int *p=nullptr;

p=&x;
y=*p;
x=y;

cout<<x<<" "<<y;
}

How can I do?

Axl_TT
  • 13
  • 3

3 Answers3

4
void swap(int *x, int *y)
{
    int tmp;

    tmp = *x;
    *x = *y;
    *y = tmp;
}
int main()
{
    int a = 7, b = 3;

    std::cout << a << ' ' << b << '\n';
    swap(&a, &b);
    std::cout << a << ' ' << b << '\n';
}

Also consider std::swap

lost_in_the_source
  • 10,998
  • 9
  • 46
  • 75
1

The problem is that you are overwriting the original value of y with the value of x, then copying that value back to x.

What you want to do is to stash the value of x, copy the value of y to it, then move the stashed value to y.

Since you are trying to do this with pointers, you should probably change the operation x=y to use pointers as well. Here is an example:

int main(int argc, char *argv[])
{
    int x=5, y=10, stash;
    int *px, *py;

    cout << "Before: x=" << x << " y=" << y;

    px = &x;
    py = &y;

    stash = *px;
    *px = *py;
    *py = stash;

    cout << "After: x=" << x << " y=" << y;
}
Mad Physicist
  • 107,652
  • 25
  • 181
  • 264
-1

Here's another good old way of swapping values of two variables without using a third variable:

x = x + y;  // x now becomes 15
y = x - y;  // y becomes 10
x = x - y;  // x becomes 5

Yet another way of doing it, as already pointed out by someone is using xor.

x = x ^ y;  // x now becomes 15 (1111)
y = x ^ y;  // y becomes 10 (1010)
x = x ^ y;  // x becomes 5 (0101)

There is also a one liner way of doing it:

x = x + y - (y = x);

You don't necessarily need pointers.

Limitation - As all the commentators have indicated, there is a limitation with all these approaches. These approaches may give you incorrect results if the sum of x and y exceeds INT_MAX (which may be equal to 2,147,483,647 on positive side and -2,147,483,648 on a negative side on a 32 bit system) AND your C implementation doesn't wrap around signed integers. However for your program, if you are working with relatively smaller numbers or your C implementation wraps around signed integers too, you should not have any issues using any of these 3 methods. But if you think that the sum might exceed INT_MAX, you can use the old fashioned temp variable approach to swap the values or use the standard swap function provided in the libraries.

VHS
  • 9,534
  • 3
  • 19
  • 43
  • 1
    unless you overflow your signed ints... which is UB. – Karoly Horvath Apr 05 '17 at 15:52
  • minus 1 for overflow vulnerabilities – lost_in_the_source Apr 05 '17 at 15:53
  • @stackptr, this was to just get OP going with the basics. Look at the alternate answer I have posted. – VHS Apr 05 '17 at 15:58
  • http://stackoverflow.com/questions/11644362/are-the-results-of-bitwise-operations-on-signed-integers-defined – Karoly Horvath Apr 05 '17 at 16:01
  • @VHS XOR swap isn't good either. Just stick to the straightforward temp-variable approach – lost_in_the_source Apr 05 '17 at 16:10
  • 2
    Three examples of **horrible** code. All of these have serious drawbacks. These hacks do not belong in anyone's notion of "basics". – Pete Becker Apr 05 '17 at 18:14
  • @PeteBecker, you might want to check the definition of `horrible` and `basics`. Yes, there is a limitation with this code which is now documented in the answer. – VHS Apr 05 '17 at 20:39
  • The third one looks a sequencing issue with UB to me. PS: Accepting and learning from your mistakes is a virtue. – Karoly Horvath Apr 05 '17 at 22:13
  • 1
    The "old fashioned temp variable approach" is the right way to do this. And note that the **only** guarantee you have on the range of `int` variables is that they can hold values in [-32727, 32767]. Yes, you can write a long list of qualifiers explaining when this hacky code will give the right answer and when it won't, but that's just too much work and too much risk. The straightforward "old fashioned" approach works just fine, and there is simply no need for these things. Engineering is about **solving** problems, not **creating** them. – Pete Becker Apr 06 '17 at 13:24