2

I tried to swap two integer using pointers...

#include<stdio.h>
int main()
{
int a,b,*i,*j;
printf("Enter two integer:");
scanf("%d%d",&a,&b);
i=&a;
j=&b;
a=*j;
b=*i;
printf("\n %d \t %d",a,b);
return 0;
}

The input is

12 45

The output is

45 45

After some trials, I found that If I assigned the b=*i first and then assigned a=*j, the first integer i.e 12 is repeating..

Why this happens ? In my understanding of pointer, this is what I've done. I've assigned the *j (i.e value of variable stored in address of a) to b and *i(i.e value of variable stored in address of b) to a..

Please explains what really happens in this program..

2
  • It is unclear whether you are looking for a C or C++ solution. Because several answers already gave C++ solutions, I wouldn't remove the C++ tag. Please next time be specific on C++ XOR C, don't mix those languages up. Commented Oct 26, 2013 at 18:22
  • He said "C" in title, that's quite definitive I'd say. Commented Oct 26, 2013 at 18:27

6 Answers 6

4

Conceptually, this is what you want to do:

int temp = a; //temp <- 12
a = b;        //a <- 45
b = temp;     //b <- 12

Conceptually, this is what you're doing:

a = b; //a <- 45
b = a; //b <- 45

You can do this "elegantly" if you're using C++11:

std::tie(b, a) = std::make_tuple(a, b);
Sign up to request clarification or add additional context in comments.

1 Comment

+1 I like the C++11 variation. Could use an explanation why tie(b,a) = tie(a,b) wouldn't work though. And possibly a demo like tie(d,a,b,c) = make_tuple(a,b,c,d) to to show how it's more versatile. (For bonus points, a comparison of generated assembly code to show that it compiles to the same instructions)
3

This is what happens:

i=&a; //i points to a
j=&b; //j points to b
a=*j; //assign the value of b to a
b=*i; //assign the value of a, which has been assigned to the value of b in the previous step

This is a workaround:

int temp = a;
a = b;
b = temp;

2 Comments

Thanks.. I don't know how I forgot this simple logic. :D
Please chose an answer which most suitably answers your question and accept it as correct, so that it is shown at the very top.
3

Simples:

#include<iterator>
std::iter_swap(i,j);

Or indeed

#include<algorithm>
std::swap(a,b);

Or for purists

using std::swap;
swap(a,b); // allow for ADL of user-defined `swap` implementations

Comments

2
a=*j;
b=*i;

i points address of a after first statement a value become 45 and next assigning a value to b so b also becomes 45

      addressof `a` 
      ^
i-----|

      addressof 'b' 
      ^ 
j-----|

Now when you make change to a then value dereferenced by i also changes.

simply use a temporary variable

int temp;
temp=*i;
*i=*j;
*j=temp;

Comments

2
i=&a; //i points to a
j=&b; //j points to b
a=*j; // this statement is equvivalent to a = b; (since *j and b both are same)
so a = 45;
now
b=*i; // this statement is equvivalent to b = a; (since *i and a both are same) 
but a value is changed to 45 in previous statement, so the same 45 is assigned to b variable also 

You could use temp variable

1 Comment

Yeah.. I forgot this simple logic.. Thanks :D
0

This is because i points to a, so as soon as you assign something to a, original value stored in a is lost. You can either introduce third variable temp to store value between assignments (like in one of the answers posted), or do some trick like

a=a+b;
b=a-b;
a=a-b;

to avoid using third variable.

EDIT This technique would work only for unsigned integer type.

6 Comments

integer overflow lurking
@sehe Wouldn't it still work despite of overflow - since it is circularly overflowing, so substructions would take care of overflow?
@IlyaKobelevskiy except signed integer overflow is undefined
@IlyaKobelevskiy: C++ defines only unsigned overflow.
Overflowing does reliably wrap-around only for unsigned data types. For signeds, it is undefined behavior.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.