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