swapping

This is a discussion on swapping within the C Programming forums, part of the General Programming Boards category; can any one tell me how to write prog to swap two var without usingthe third var....

  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,786


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

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >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, 11:28 AM
  3. C bubble sort?
    By fredanthony in forum C Programming
    Replies: 11
    Last Post: 02-13-2006, 08:54 PM
  4. Swapping pointers
    By VanJay011379 in forum C++ Programming
    Replies: 3
    Last Post: 07-30-2002, 12:57 PM
  5. swapping within a char pointer array
    By Mario in forum C++ Programming
    Replies: 3
    Last Post: 05-25-2002, 01:31 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21