#include<stdio.h>
void fun(int **a,int **b)
{
int **k;
k=a;
a=b;
b=k;
}
int main()
{
int a=3,b=6,*x=&a,*y=&b;
fun(&x,&y);
printf("%d %d",a,b);
return 0;
}
I swap the address,why it still outputs 3 6. So what's the correct way to swap the two address?
p =...
never persists across the function call. Like everything else in C, if you want to change the caller's data, dereference the pointers you're given. k
should be int *
, and the assignments should be int *k = *a; *a = *b; *b = k;
int *k = *a; *a = *b; *b = k;
. that's it. Note also this changes nothing about the original a
and b
. All your swapping at this point are pointer values.
This code swaps two integers
#include<stdio.h>
void fun(int *a,int *b)
{
int k = *a;
*a = *b;
*b = k;
}
int main()
{
int a=3,b=6;
printf("%d %d\n",a,b);
fun(&a,&b);
printf("%d %d\n",a,b);
return 0;
}
Output
3 6
6 3
Whereas, this code swaps two pointers. The integers they point to (a and b) remain as-is.
#include<stdio.h>
void fun(int **a,int **b)
{
int *k = *a;
*a = *b;
*b = k;
}
int main()
{
int a=3,b=6,*x=&a,*y=&b;
printf("%d %d\n",a,b);
printf("%d %d\n",*x,*y); // note using x and y with deref
fun(&x,&y);
printf("%d %d\n",a,b);
printf("%d %d\n",*x,*y); // note using x and y with deref
return 0;
}
Output
3 6
3 6
3 6
6 3
Lets make it clear from the first expression.
int a=3,b=6,*x=&a,*y=&b;
Now breaking it down.
int a = 3, b = 6
Here a
and b
are two memory locations. Say a
is 10000000
and b
is 10010000
. So, after we write a = 3, b = 6
the value 3
takes the place in memory location 10000000
and 6
in 10010000
. Now,
*x=&a,*y=&b;
means, x
is an integer pointer that has the address of integer variable a
which is 10000000
and y
has the address of b
which is 10010000
. Note that, both x
and y
are also variable which can store the address as value of any integer variable. x
and y
has locations in memory let say 11000000
and 11011111
. Now,
fun(&x,&y);
You are passing the address of x
and y
to the function fun
. It looks like fun(11000000, 11010000)
. Note that, you are not passing the address of a
and b
! In function fun
these addresses are in a
( pa
for clarity ) and b
( pb
for clarity),
void fun(int **a,int **b)
{
int **k;
k = a;
a = b;
b = k;
}
Here, a
( pa
), b
( pb
) and k
are pointers of pointer. They can hold the address of an integer pointer. a
( pa
) and b
( pb
) are holding the address of x
and y
respectively. Now when you swapping a
( pa
) and b
( pb
) you actually swapping the addresses ( of x and y ) in a
( pa
) and b
( pb
). So, after the function fun
finishes it's execution a
( pa
) is pointing y
and b
( pb
) is pointing x
. In main function, nothing actually happened. When you do the following,
void fun(int **a, int **b)
{
int *k;
k=*a;
*a = *b;
*b = k;
}
it swaps the address in x
and y
and they now pointing to b
and a
(in main function).
Now the final version comes, If you would do the following, it actually make you happy ,
void fun(int **a, int **b)
{
int k;
k=**a;
**a = **b;
**b = k;
}
I hope you understand it. Happy coding !
Try this to swap the values to which *a and *b of fun() points
void fun(int **a, int **b)
{
int k;
k = **a;
**a = **b;
**b = k;
}
Instead of this:
int **k;
k=a;
a=b;
b=k;
you should have tried this:
int *k;
k=*a;
*a=*b;
*b=k;
And yeah, you should print *x
and *y
, because those are the variables you swap, not a
and b
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.