Thread: Please help me! Pointer!

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    6

    Please help me! Pointer!

    I run this code and the program give me this result. But i don't know how the program can have this result. Please explain for me.
    Code:
    #include <stdio.h> 
    
    void swap1(int *i1,int *i2) { 
       int tmp; 
    
       tmp=*i1; 
       *i1=*i2; 
       *i2=tmp; 
    } 
    
    void swap2(int i1,int i2) { 
       int tmp; 
    
       tmp=i1; 
       i1=i2; 
       i2=tmp; 
    } 
    
    main(){ 
       int i1,i2; 
    
       i1=100; 
       i2=200; 
       printf("i1=%d i2=%d\n",i1,i2); 
    
       swap1(&i1,&i2); 
       printf("i1=%d i2=%d\n",i1,i2); 
    
       swap2(i1,i2); 
       printf("i1=%d i2=%d\n",i1,i2); 
    }
    The result is:
    i1=100 i2=200
    i1=200 i2=100
    i1=200 i2=100

    When i run swap1 i think that i1=200 and i2=100. So when i run swap2 they must be i1=100 and i2=200???

  2. #2
    Registered User
    Join Date
    Feb 2008
    Posts
    146
    In swap1 you are passing the arguements by reference and hence the swap occurs, where in swap2 you are passing the arguements by value : that means i1 and i2 are created locally in scope of swap2 , the values you pass are copied there and swaped but not written back to i1 and i2 of main..

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    6
    Thanks. And about this one, i don't know why they define tmp in the swap but not use it? So why there is tmp here?
    Code:
    #include <stdio.h> 
     
    void swap(int *i1,int *i2) { 
      int tmp; 
     
      *i1=*i1^*i2; 
      *i2=*i1^*i2; 
      *i1=*i1^*i2; 
    } 
     
    main(){ 
      int i1,i2; 
     
      i1=100; 
      i2=200; 
     
      printf("i1=%d i2=%dĽn",i1,i2); 
     
      swap(&i1,&i2); 
      printf("i1=%d i2=%dĽn",i1,i2); 
    }

  4. #4
    Registered User
    Join Date
    Feb 2008
    Posts
    146
    What is the purpose of this program?
    It definitely does not perform a swap...whatever it does.. remove tmp, you'll still get the same result ...

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    tmp is not needed in this case. It's probably simply an omission that it wasn't removed.

    The "xor-swap" is one of those "clever" things that people bring out every now and again. In my opinion, it should stay a "show-piece", rather than become something you start using in real-life. At least if we are talking about modern-ish processors, as a temporary variable will be faster than the three XOR operatons.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Registered User
    Join Date
    Feb 2008
    Posts
    146
    cool........ I didn't realize, that performs a swap.. , and I am surprised why I haven't come across XOR-swap till now...

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by edesign View Post
    cool........ I didn't realize, that performs a swap.. , and I am surprised why I haven't come across XOR-swap till now...
    It is one of those "clever" things that is rarely much use in real life, as I said.

    It is used to show people "Look how clever I am" (except of course, that's only valid if no one else told you about it, or gave you clues).

    More info:
    XOR swap algorithm - Wikipedia, the free encyclopedia

    It also explains much better than I do what the good/bad points of this is.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Registered User
    Join Date
    Feb 2008
    Posts
    146
    Thanks ... that was quite informative ... I enjoyed reading that
    Last edited by edesign; 05-26-2009 at 08:49 AM.

  9. #9
    Registered User
    Join Date
    May 2009
    Posts
    6
    Thanks for your reply. They are very useful. Thank you very much.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  2. Quick Pointer Question
    By gwarf420 in forum C Programming
    Replies: 15
    Last Post: 06-01-2008, 03:47 PM
  3. Parameter passing with pointer to pointer
    By notsure in forum C++ Programming
    Replies: 15
    Last Post: 08-12-2006, 07:12 AM
  4. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM