Thread: Swap function and pointers

  1. #1
    Registered User
    Join Date
    Dec 2011
    Location
    College Station, Texas, United States
    Posts
    2

    Swap function and pointers

    Code:
    #include <stdio.h>
    void swap1(int, int);
    void swap2(int*, int*);
    int main()
    {
          int i = 1, j = 2;
          swap1(i, j);
          printf("%d and %d\n", i, j); /* For Problem #57 */
          swap2(&i, &j);
          printf("%d and %d\n", i, j); /* For Problem #58 */
          return 0;
    }
    void swap1(int i, int j)
    {
          int tmp = i;
          i = j;
          j = tmp;
    }
    void swap2(int *i, int *j)
    {
          int tmp = *i;
          *i = *j;
          tmp = *j;
    }
    According to my instructor, the first printf should output "1 and 2" and the 2nd printf will output "2 and 2". I'm not seeing it. I thought the first one would swap them and output "2 and 1", and as for the second printf, I have no idea how to work with these pointers. Any help will be greatly appreciated.

  2. #2
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > According to my instructor, the first printf should output "1 and 2" and the 2nd printf will output "2 and 2".
    What you need is a new instructor if they seriously think 2 and 2 is the result.

    First one does nothing to the variables, and the second one swaps.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Dec 2011
    Location
    College Station, Texas, United States
    Posts
    2
    thanks guys, I think I get it now.

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Salem View Post
    First one does nothing to the variables, and the second one swaps.
    Look carefully at the swap2() function in the OP, Salem. It does not swap. The instructor is correct about what output would be produced.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by grumpy View Post
    Look carefully at the swap2() function in the OP, Salem. It does not swap. The instructor is correct about what output would be produced.
    Unfortunatly, that's because the swap function is incorrect.

    Where did they get THIS teacher?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 11
    Last Post: 11-07-2010, 01:17 PM
  2. swap function
    By c_lady in forum C Programming
    Replies: 4
    Last Post: 06-09-2010, 02:27 PM
  3. Swap 2 words by pointers
    By jaxlle in forum C Programming
    Replies: 2
    Last Post: 01-15-2007, 02:49 PM
  4. swap function
    By SpEkTrE in forum C Programming
    Replies: 8
    Last Post: 02-18-2005, 01:38 PM
  5. Problem writing swap using pointers
    By joshdick in forum C++ Programming
    Replies: 1
    Last Post: 02-29-2004, 10:06 PM