Aug 17, 2011 at 12:07pm
What is the difference between these two swap() methods:
1. void swap(int **a, int **b)
{
int *temp;
temp = *a;
*a = *b;
*b = temp;
}
2. void swap(int* a, int* b)
{
int* temp;
temp = a;
a = b;
b = temp;
}
Aug 17, 2011 at 12:29pm
What do you mean doesn't do anything. the program compiles the same way. The only thing that i see is that #2 doesn't use dereferencing in its method.
Aug 17, 2011 at 12:32pm
#2 works on the copies of the pointers and does not effect the data 'outside' of the function.
this swaps the contents of what a and b point to.
1 2 3 4 5 6 7
|
void swap(int* a, int* b)
{
int temp;
temp = *a;
*a = *b;
*b = temp;
}
|
Last edited on Aug 17, 2011 at 12:41pm