Thread: swapping help

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    96

    swapping help

    i want to swap the cotent of x and y how would i do that in pointer

    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<stdlib.h>
    
    main()
    {     
      int x=3;
      int y=4;
      int *ptr1=&x;
      int *ptr2=&y;
      int temp;
    
      temp=*ptr1;
      *ptr1=*ptr2;
      *ptr2=temp;
      
      printf("x is %i y is %i ",ptr1,ptr2);
      
      
      getch();
      
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You appear to be doing the swapping correct. You just need to print x and y.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    96
    what is wrong with my print statement

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You are printing the pointers, not what they point to, yet what you want to print is what they point to, i.e., x and y.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

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. Endian swapping (byte-order reversal) on a file
    By Molokai in forum C++ Programming
    Replies: 4
    Last Post: 10-16-2009, 07:22 AM
  3. Swapping strings in an array of strings
    By dannyzimbabwe in forum C Programming
    Replies: 3
    Last Post: 03-03-2009, 12:28 PM
  4. C bubble sort?
    By fredanthony in forum C Programming
    Replies: 11
    Last Post: 02-13-2006, 09:54 PM
  5. swapping within a char pointer array
    By Mario in forum C++ Programming
    Replies: 3
    Last Post: 05-25-2002, 01:31 AM