# Thread: confused with scope&pointer

1. ## confused with scope&pointer

why does the output become like this:
x=2, y=3, z=4
x=3, y=2, z=4
x=4, y=2, z=3 <<< referring to this
x=2, y=4, z=3

when int_swap(&x,&z)
then int_swap will be int_swap(2,4) ?
then
t = 2;
*x = 4;
*y = 2;
so x=4, y=3, z=2 ??

What am i misunderstanding?
same with the final output, it doesnt make sense!

:-(

Code:
```#include <stdio.h>

void int_swap(int*, int*);

int main(int argc, char **argv) {
int x=2, y=3, z=4;
printf( "main: x=%2d, y=%2d, z=%2d\n", x, y, z);
int_swap(&x, &y);
printf( "main: x=%2d, y=%2d, z=%2d\n", x, y, z);
int_swap(&x, &z);
printf( "main: x=%2d, y=%2d. z=%2d\n", x, y, z);
int_swap(&y, &x);
printf( "main: x=%2d, y=%2d, z=%2d\n", x, y, z);
return 0;
}

void int_swap( int *x, int *y ) {
int t;
t = *x;
*x = *y;
*y = t;
}```

2. x=2 y=3 z=4.
swop x and y
x=3 y=2 z=4
swop x and z
x=4 y=2 z=3
swop y and x
x=2 y=4 z=3.

wheres the problem?

3. yeah when it swaps x and z
how does the swap function work
thats the part i dont get..

it will do int_swap( 2, 4 )
t = 2;
x = 4;
y = 2;

so x=4, y=3, z=2 ??

why is z and y switched?

4. Code:
```void int_swap( int *x, int *y ) {
int t;
t = *x;
*x = *y;
*y = t;
}```
Line 1: function doesn't returns value, receive two pointers x and y. Line 2: declare a variable t; line 3: put value of x in variable t; Line 4: change value of x with value of y; Line 5: change value of y with value of t that holds the first value of x;

Now simpler, let's say X is a pointer to a variable that holds 5, and Y is a pointer to a variable that holds 7.

t = 5;
x = 7; //change the value of the variable that x is pointing
y = 5; //change the value of the variable that y is pointing

5. lets say x=2, y=3, z=4 from above

int_swap( &x, &z )

x points to 2?
z points to 4?

so then
t = *x which is t = 2?
*x = *y which is *x = 4?
*y = *x which is *y = 2?

am i correct? so then x=4, y=3, z=2?

6. //lets say x=2, y=3, z=4 from above
ok let's see
int x = 2, y = 3, z = 4;

//int_swap( &x, &z )
passing the adress of x and z;

//x points to 2?
yes.

//z points to 4?
yes.

//t = *x which is t = 2?
right.

//*x = *y which is *x = 4?
you mean Z or Y? I don't see why here, if you mean Z yes.

//*y = *x which is *y = 2?
no... now should be *z = t; and z will be 2; well forget y here, cuz I don't see it. what example you mean?

//am i correct? so then x=4, y=3, z=2?
yes.

7. i had some idea what was happening
however with my first post which contains the code, the output is not what i had calculated.

have i missed out on something?
somehow the values have been switched and i dont know why.

8. Code:
```#include <stdio.h>

void int_swap(int*, int*);

int main(int argc, char **argv) {
int x=2, y=3, z=4;
//we did nothing until here, so value is normal 2,3,4;
printf( "main: x=%2d, y=%2d, z=%2d\n", x, y, z);
//we are going to swap 2 with 3 (x and y);
int_swap(&x, &y);
//not it should print 3, 2, 4;
printf( "main: x=%2d, y=%2d, z=%2d\n", x, y, z);
//going to swap 3 and 4 (x and z);
int_swap(&x, &z);
//now it should print 4, 2, 3;
printf( "main: x=%2d, y=%2d. z=%2d\n", x, y, z);
//swap 2 and 4 (y and x);
int_swap(&y, &x);
//now it should print 2, 4, 3;
printf( "main: x=%2d, y=%2d, z=%2d\n", x, y, z);
getchar();
return 0;
}

void int_swap( int *x, int *y ) {
int t;
t = *x;
*x = *y;
*y = t;

/*
first time: swapping 2 and 3; x = 2, y = 3;
t = 2;
x = 3;
y = 2 (t - our temporary variable);

second time: swapping 3 and 4; x = 3; z = 4;
t = 3;
x = 4;
z = 3 (t - our temporary variable);

third time: swapping 2 and 4; y = 2; x = 4;
t = 2 (LOOK HERE, YOU PASSED Y FIRST, SO T IS POINTING TO Y) NO MATTER IF THE NAME IS *x
HE IS POINTING TO Y LOOK AT THE POSITION.
*/
}
}```
I think I understand your error:
look here:
Code:
`int_swap(&y, &x);`
to your calc be right, you should pass x first

Popular pages Recent additions