tiger0516
asked on
Pointer question
I am learning C and I feel a bit confused by pointer. i write this simple code to test:
My idea is that, to swap two values, I can either
1) swapping two values stored in vars
2) swapping values' pointers
For example, I have two vars, a and b. Pointers to a and b are ffda and ffdc (in my local machine).
Innitially, a with adr ffda has the value of 5; b with addr ffdc has the value of 10;
after swap1 function, a with adr ffda has the value of 10; b with addr ffdc has the value of 5;
But swap2 does not work, I had though it will changes a's addr to ffdc, and b's to ffda. I made mistakes somewhere. Could you please help me ?
Thanks
#include <stdio.h>
// Wrong
void swap(int x,int y)
{
int temp=x;
x=y;
y=temp;
}
//Swap: By swappings value presented by *x and *y
void swap1(int *x,int *y)
{
int temp=*x;
*x=*y;
*y=temp;
}
//Swap: By swapping pointers of x and y
void swap2(int *x,int *y)
{
int temp=&*x;
&*x=&*y;
&*y=temp;
}
main ()
{
int a=5;
int b=10;
printf("Before swap, a=%d,b=%d\n",a,b);
printf("Before swap, address of a=%x,b=%x\n",&a,&b);
swap(a,b);
printf("After simple swap, a=%d,b=%d\n",a,b);
printf("After simple swap, address of a=%x,b=%x\n",&a,&b);
swap1(&a,&b);
printf("After simple swap, a=%d,b=%d\n",a,b);
printf("After simple swap, address of a=%x,b=%x\n",&a,&b);.
swap2(&a,&b);
printf("After simple swap 2, a=%d,b=%d\n",a,b);
printf("After simple swap 2, address of a=%x,b=%x\n",&a,&b);
}
My idea is that, to swap two values, I can either
1) swapping two values stored in vars
2) swapping values' pointers
For example, I have two vars, a and b. Pointers to a and b are ffda and ffdc (in my local machine).
Innitially, a with adr ffda has the value of 5; b with addr ffdc has the value of 10;
after swap1 function, a with adr ffda has the value of 10; b with addr ffdc has the value of 5;
But swap2 does not work, I had though it will changes a's addr to ffdc, and b's to ffda. I made mistakes somewhere. Could you please help me ?
Thanks
#include <stdio.h>
// Wrong
void swap(int x,int y)
{
int temp=x;
x=y;
y=temp;
}
//Swap: By swappings value presented by *x and *y
void swap1(int *x,int *y)
{
int temp=*x;
*x=*y;
*y=temp;
}
//Swap: By swapping pointers of x and y
void swap2(int *x,int *y)
{
int temp=&*x;
&*x=&*y;
&*y=temp;
}
main ()
{
int a=5;
int b=10;
printf("Before swap, a=%d,b=%d\n",a,b);
printf("Before swap, address of a=%x,b=%x\n",&a,&b);
swap(a,b);
printf("After simple swap, a=%d,b=%d\n",a,b);
printf("After simple swap, address of a=%x,b=%x\n",&a,&b);
swap1(&a,&b);
printf("After simple swap, a=%d,b=%d\n",a,b);
printf("After simple swap, address of a=%x,b=%x\n",&a,&b);.
swap2(&a,&b);
printf("After simple swap 2, a=%d,b=%d\n",a,b);
printf("After simple swap 2, address of a=%x,b=%x\n",&a,&b);
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
forgot to say thanks :-)
ASKER
Should
void swap2(int **x,int **y)
{
int *temp;
temp = *x;
*x = *y;
*y = temp;
}
be
void swap2(int **x,int **y)
{
int *temp;
temp = **x;
**x =* *y;
**y = temp;
}
?