Thread: swapping

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    1

    swapping

    can any one tell me how to write prog to swap two var without usingthe third var.

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793


    This was discussed about 2 weeks ago...do a search please

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >can any one tell me how to write prog to swap two var without usingthe third var.
    There's no reasonable point to using this technique, but for integral values only you can use binary XOR:
    Code:
    void swap ( int *a, int *b )
    {
      *a ^= *b;
      *b ^= *a;
      *a ^= *b;
    }
    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Monitoring Page Swapping?
    By Cell in forum Linux Programming
    Replies: 10
    Last Post: 06-13-2010, 12:16 PM
  2. Swapping strings in an array of strings
    By dannyzimbabwe in forum C Programming
    Replies: 3
    Last Post: 03-03-2009, 12:28 PM
  3. C bubble sort?
    By fredanthony in forum C Programming
    Replies: 11
    Last Post: 02-13-2006, 09:54 PM
  4. swapping within a char pointer array
    By Mario in forum C++ Programming
    Replies: 3
    Last Post: 05-25-2002, 01:31 AM