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;
}
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.
#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