This is what I have so far...am I on the right track? Please help!!
void intpswap(int *xp, int *yp)
{
int t = *xp;
*xp = *yp;
*yp = t;
}
On the right track with some distance still to cover :)
with
You are assigning the value '1' to the int 't'
with
you are assigning the value that yp points to, 2, to the memory space that xp points to, x. So x now equals 2
with
you are assigning the value at t, 1, to where yp points to, y. So now y = 1
The original pointers xp and yp have not changed. In order to change them we have to pass a reference to them:
- void intpswap(int **xp, int **yp)
-
{
-
int *t = *xp;
-
*xp = *yp;
-
*yp = t;
-
}
-
-
int main() {
-
int x=1,y=2;
-
int *xp,*yp;
-
-
xp = &x;
-
yp = &y;
-
intpswap(&xp, &yp);
-
-
x=3;y=4;
-
printf("*xp = %d, *yp = %d\n",*xp,*yp);
-
-
getchar();
-
return 0;
-
}
-
-
see if you can tell us what is happening here