swap

This is a discussion on swap within the C Programming forums, part of the General Programming Boards category; Consider this C code to swap two integers and these five statements: the cose void swap(int *px,int *py){ *px=*px-*py; *py=*px+*py; ...

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    10

    swap

    Consider this C code to swap two integers and these five statements: the cose

    void swap(int *px,int *py){
    *px=*px-*py;
    *py=*px+*py;
    *px=*py-*px;
    }
    statement 1: Will generate a compiler error
    statement 2: May generate a segmentation fault in run time depending on the arguments passed

    statement 3: Correctly implement the swap procedure for all input pointers referring to integers stored in memory locations accessible to the process

    statement 4: implement the swap procedure for some but not all valid input

    statement 5: May add or subtract integers and pointers

    Which of the above statement is correct ?
    (A) statement 1
    (B) statement 2 & statement 3
    (C) statement 2 & statement 4
    (D) statement 2 & statement 5

    I think only statement 3 is correct but it is not in option.

  2. #2
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    methinks (B)

  3. #3
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    Methinks you should do your own homework.

  4. #4
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,246
    The code correctly swaps the two values, assuming no integer wrap-around.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  5. #5
    Registered User
    Join Date
    Nov 2009
    Location
    wuhan ,hubei ,china
    Posts
    7
    B is right

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. linked list swap function
    By Axel in forum C Programming
    Replies: 32
    Last Post: 01-16-2011, 08:40 AM
  2. Atomic compare and swap.
    By Maz in forum C Programming
    Replies: 3
    Last Post: 04-07-2009, 02:47 PM
  3. using swap to make assignment operator exception safe
    By George2 in forum C++ Programming
    Replies: 9
    Last Post: 01-10-2008, 05:32 AM
  4. Double Link List Swap function
    By kennny2004 in forum C++ Programming
    Replies: 1
    Last Post: 04-12-2006, 11:52 AM
  5. Skipping DX swap chain node - not good...
    By Magos in forum Game Programming
    Replies: 0
    Last Post: 03-02-2006, 07:37 PM

Tags for this Thread


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