i have encountered two methods of swapping two integer values and I’m confused about the difference.
Method1(temp is a pointer):
void Swap(int *a, int *b)
{
int *temp = a;
*a = *b;
*b = *temp;
}
Method2(temp is not a pointer):
void Swap(int *a, int *b)
{
int temp = *a;
*a = *b;
*b = temp;
}
I don’t understand the difference between two methods, and is one method better than the other?
CodePudding user response:
The first method does not work, because the former contents of the int pointed to by a is not saved. The initializationtemp = a copies only the pointer.
You might want to learn how to debug small programs to see what happens.
CodePudding user response:
Perhaps it's easier to understand the first (wrong) function if we draw it out?
When the function is called, it will be something like this:
--- ------------- | a | ---> | value for a | --- ------------- --- ------------- | b | ---> | value for b | --- -------------
Then after the initialization of temp you will have this:
--- -------------
| a | ----- ---> | value for a |
--- | -------------
|
------ |
| temp | --/
------
--- -------------
| b | ---> | value for b |
--- -------------
You have two pointers, pointing to the same location.
Then you do the assignment
*a = *b;
which leads to this situation:
--- -------------
| a | ----- ---> | value for b |
--- | -------------
|
------ |
| temp | --/
------
--- -------------
| b | ---> | value for b |
--- -------------
As can easily be seen, the value for a is lost.
PS.
Whenever you have troubles with pointers, and can't really visualize what's happening, I recommend you take a step back, fetch a pencil and paper, and draw it all out.
CodePudding user response:
what happens in the first method is that temp points to the same address as a; upon changing *a to what b points to, temp now points to what b (and a) now point to.
after the statement *b=*temp, you changed nothing, as if you have said *b=*b;
CodePudding user response:
In this function
void Swap(int *a, int *b)
{
int *temp = a;
*a = *b;
*b = *temp;
}
after this record
int *temp = a;
the both pointers a and temp point to the same object that was changed by the value stored in the object pointed to by the pointer b.
*a = *b;
So as a result the value stored in the object pointed by the pointer a was lost.
It seems that when you copied the function you was not attentive and made a typo. The function will make a sense if to make the following changes
void Swap(int **a, int **b)
{
int *temp = *a;
*a = *b;
*b = temp;
}
Such a function is designed to swap two pointers of the type int *.
CodePudding user response:
void Swap(int *a, int *b)
{
int *temp = a;
*a = *b;
*b = *temp;
}
'a' and 'b' in memory look like this:
a --> address of an integer
b --> address of an integer
In the first function you create
int *temp = a
which looks like this in memory
temp --> a
temp will store the address of the integer that 'a' points too.
then you do
*a = *b
a and b currently look like this:
a --> address of an integer
b --> address of an integer
the operation will get the integer value from "address of an integer" for 'b' and store it in "address of an integer" for 'a'
*** Now 'a' and 'b' store the same integer value ***
At this stage you should remember that temp also points to 'a' (and we have just edited 'a').
Lastly you do
*b = *temp
which will basically do nothing since
temp --> a(temp points to a)- 'a' and 'b' have the same integer value
- so doing
*b = *tempjust assigns *b with the value it already has.
So in the end this function has just assigned 'a' and 'b' with the same value
void Swap(int *a, int *b)
{
int temp = *a;
*a = *b;
*b = temp;
}
This function will work unlike the previous one because it will store the integer value of 'a' into temp, then it will edit *a, then edit b with the previously saved value of temp.
