Thread: confused with scope&pointer

  1. #1
    cman
    Guest

    Unhappy 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. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    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?
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    cman
    Guest
    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. #4
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    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. #5
    cman
    Guest
    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. #6
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    //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.
    Last edited by Vber; 03-23-2003 at 03:26 AM.

  7. #7
    cman
    Guest
    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. #8
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    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 subscribe to a feed

Similar Threads

  1. New to C++ and confused by boolean and ifs and else
    By jconner in forum C++ Programming
    Replies: 10
    Last Post: 08-02-2006, 03:29 AM
  2. Confused
    By (TNT) in forum C# Programming
    Replies: 1
    Last Post: 11-23-2005, 04:49 PM
  3. why wont this compile?!? :confused:
    By jdude in forum C++ Programming
    Replies: 5
    Last Post: 11-25-2004, 01:13 AM
  4. confused.. in selecting my line of deapth
    By jawwadalam in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 05-04-2003, 01:21 PM
  5. Extern Question, really confused
    By SourceCode in forum C Programming
    Replies: 10
    Last Post: 03-26-2003, 11:11 PM