Thread: Swap Function

  1. #1
    Unregistered
    Guest

    Swap Function

    Can anyone tell me real quick and fast way to write a swap function for an array of unsigned characters. I have my entire assignment done but this part and I just don't get it....Anyone?

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    42
    Code:
    int i;
    int array[size];
    
    void swap(your parameters) {
              i = array[swapindex];
              array[swapindex] = newvalue;
              array[indextoswap] = i;
    }
    Basically just use a temporary unsigned integer to hold the value of the index that you are swapping with a new value. Is that your problem, or are you having problems passing the array to a function as a parameter?
    http://www.KBeutler.com

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Can anyone tell me real quick and fast way to write a swap
    >function for an array of unsigned characters.
    Code:
    typedef unsigned char uchar;
    
    void swap ( uchar *x, uchar *y )
    {
      uchar temp = *x; 
      *x = *y;
      *y = temp;
    }
    The call to this function is
    swap ( &array[x], &array[y] );

    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. Troubleshooting Input Function
    By SiliconHobo in forum C Programming
    Replies: 14
    Last Post: 12-05-2007, 07:18 AM
  3. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 08:56 AM
  4. Question..
    By pode in forum Windows Programming
    Replies: 12
    Last Post: 12-19-2004, 07:05 PM
  5. I need help with passing pointers in function calls
    By vien_mti in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 10:00 AM