Point and Swap

This is a discussion on Point and Swap within the C++ Programming forums, part of the General Programming Boards category; This code successfully swaps the variables, but I'm wondering why the address for the first element (2) is greater than ...

  1. #1
    Registered User Kayoss's Avatar
    Join Date
    Sep 2005
    Location
    California
    Posts
    53

    Point and Swap

    This code successfully swaps the variables, but I'm wondering why the address for the first element (2) is greater than the address for the second element (3)?

    Code:
    #include <iostream>
    
    using namespace std;
    
    void swap( int * const, int * const ); //  prototype
    
    int main()
    {
    
       int array[2] = {2,3}; 
    
       int *arrayPTR = array; 
       int *arrayPTR1 = &array[1]; 
    
    
       cout << *arrayPTR << "  " << &arrayPTR << endl; // first element and address
       cout << *arrayPTR1 << "  " << &arrayPTR1 << endl; // second element and address
       cout << "SWAP" << endl;
    
    
       // the following code swaps elements in array, defined after   function main
    
       swap( &array[0], &array[0 + 1] );
    
    
       cout << *arrayPTR << "  " << &arrayPTR << endl;
       cout << *arrayPTR1 << "  " << &arrayPTR1 << endl;
    
    
    return 0;
    
    }
    
    // defines swap function, swaps values in array
    
    void swap( int * const element1PTR, int * const element2PTR )
    {
    	int hold = *element1PTR;
    	*element1PTR = *element2PTR;
    	*element2PTR = hold;
    
    } // end function swap
    THE redheaded stepchild.

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,799
    Who says data placed in memory has to start from the bottom and move up? It's entirely possible that it could be placed in the higher addresses first and move down from there.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    On my system, when I run this program:
    Code:
    #include<iostream>
    using namespace std;
    
    int main()
    {
    	int arr[2]={2,3};
    	
    	cout<<arr<<endl
    		<<&arr[1]<<endl;
    
    	return 0;
    }
    I get this output:

    006BFDF0
    006BFDF4

  4. #4
    Registered User Kayoss's Avatar
    Join Date
    Sep 2005
    Location
    California
    Posts
    53
    Hmm I suppose it doesn't have to...it just has for me up until this point.

    I get:

    0012FF74
    0012FF70
    Last edited by Kayoss; 11-09-2005 at 02:45 PM.
    THE redheaded stepchild.

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Who says data placed in memory has to start from the bottom and move up?
    Whaaa?? All the pictures in my book have the memory going from left to right?

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Kayoss: Array members must be in an increasing order, but your program is not showing that -- it is showing the address of some pointers that hold the address you want.
    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
       int array[2] = {2,3};
       cout << &array[0] << ' ' << array[0] << '\n';
       cout << &array[1] << ' ' << array[1] << '\n';
       return 0;
    }
    
    /* my output
    0012FF84 2
    0012FF88 3
    */
    [edit]
    Code:
    #include <iostream>
    using namespace std;
    
    void swap( int * const element1PTR, int * const element2PTR )
    {
    	int hold = *element1PTR;
    	*element1PTR = *element2PTR;
    	*element2PTR = hold;
    }
    
    int main()
    {
       int array[2]   = {2,3};
       int *arrayPTR  = array;
       int *arrayPTR1 = &array[1];
    
       cout << *arrayPTR  << "  " << arrayPTR << endl; // first element and address
       cout << *arrayPTR1 << "  " << arrayPTR1 << endl; // second element and address
       cout << "SWAP" << endl;
    
       // the following code swaps elements in array, defined after   function main
       swap( &array[0], &array[0 + 1] );
       cout << *arrayPTR  << "  " << arrayPTR << endl;
       cout << *arrayPTR1 << "  " << arrayPTR1 << endl;
    
       return 0;
    }
    
    /* my output
    2  0012FF84
    3  0012FF88
    SWAP
    3  0012FF84
    2  0012FF88
    */
    Last edited by Dave_Sinkula; 11-09-2005 at 03:05 PM.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  7. #7
    Registered User Kayoss's Avatar
    Join Date
    Sep 2005
    Location
    California
    Posts
    53
    Thank you Dave!
    THE redheaded stepchild.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. rotating around object (looat)
    By jabka in forum Game Programming
    Replies: 13
    Last Post: 06-18-2008, 05:02 PM
  2. Why not use an = Operator instead of a Copy Constructor?
    By thetinman in forum C++ Programming
    Replies: 48
    Last Post: 10-15-2007, 03:58 PM
  3. 6 measly errors
    By beene in forum Game Programming
    Replies: 11
    Last Post: 11-14-2006, 10:06 AM
  4. Swap problem
    By devilhaunts in forum C Programming
    Replies: 11
    Last Post: 08-12-2006, 09:58 PM
  5. floating point operators
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 10-22-2003, 07:53 PM

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