By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,797 Members | 1,836 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,797 IT Pros & Developers. It's quick & easy.

"Swapping" two arrays like this doesn't compile because..

P: n/a
...during the call to swap_arrays the name of the array decays to a
pointer to its first element and that pointer is a temporary variable
and you cant bind a non-const reference to a temporary. Correct
analysis?

The code (that only compiles if you comment out the call to
swap_arrays()):
#include <iostream>
#include <iterator>
using namespace std;

void swap_arrays(int *&a1, int *&a2)
{
int *ptr = a1;
a1 = a2;
a2 = ptr;
}

int main()
{
int array1[] = {1, 2, 3};
int array2[] = {4, 5, 6};

swap_arrays(array1[0], array2[0]);

cout << "array1 after swap:" << endl;

copy(array1, array1 + sizeof(array1) / sizeof(array1[0]),
ostream_iterator<int>(cout, " "));

cout << endl << "array2 after swap:" << endl;

copy(array2, array2 + sizeof(array2) / sizeof(array2[0]),
ostream_iterator<int>(cout, " "));

cout << endl;
}

Feb 22 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Eric Lilja wrote:
..during the call to swap_arrays the name of the array decays to a
pointer to its first element and that pointer is a temporary variable
and you cant bind a non-const reference to a temporary. Correct
analysis?
I don't think so.
The code (that only compiles if you comment out the call to
swap_arrays()):
#include <iostream>
#include <iterator>
using namespace std;

void swap_arrays(int *&a1, int *&a2)
{
int *ptr = a1;
a1 = a2;
a2 = ptr;
}
That function will not swap arrays. It swaps objects of type pointer-to-int.
>
int main()
{
int array1[] = {1, 2, 3};
int array2[] = {4, 5, 6};

swap_arrays(array1[0], array2[0]);
You promised int*& as the parameter. What you pass is int&.
cout << "array1 after swap:" << endl;

copy(array1, array1 + sizeof(array1) / sizeof(array1[0]),
ostream_iterator<int>(cout, " "));

cout << endl << "array2 after swap:" << endl;

copy(array2, array2 + sizeof(array2) / sizeof(array2[0]),
ostream_iterator<int>(cout, " "));

cout << endl;
}

Try this:
#include <cstddef>
#include <algorithm>

using namespace std;

template < typename T, std::size_t N >
void swap ( T (&lhs) [N], T (&rhs) [N] ) {
for ( std::size_t i = 0; i < N; ++i ) {
swap( lhs[i], rhs[i] );
}
}

#include <iostream>
#include <iterator>

using namespace std;

int main ( void ) {
int array1[] = {1, 2, 3};
int array2[] = {4, 5, 6};

swap( array1, array2 );

cout << "array1 after swap:" << endl;

copy(array1, array1 + sizeof(array1) / sizeof(array1[0]),
ostream_iterator<int>(cout, " "));

cout << endl << "array2 after swap:" << endl;

copy(array2, array2 + sizeof(array2) / sizeof(array2[0]),
ostream_iterator<int>(cout, " "));

cout << endl;
}
Best

Kai-Uwe Bux
Feb 22 '07 #2

P: n/a
Eric Lilja wrote:
...during the call to swap_arrays the name of the array decays to a
pointer to its first element and that pointer is a temporary variable
and you cant bind a non-const reference to a temporary. Correct
analysis?
Yes and no.

....
void swap_arrays(int *&a1, int *&a2)
....
int array1[] = {1, 2, 3};
int array2[] = {4, 5, 6};

swap_arrays(array1[0], array2[0]);
As it is, you are passing two (int) objects where to (int*) objects are
expected. Though, even if you change this to what I *assume* you
actually meant:

swap_arrays(array1, array2);

then your above analysis is correct, and the code still won't work. You
would have the same problem with:

void swap_double(double &d1, double &d2)
{
double temp = d1;
d1 = d2;
d2 = temp;
}

....

float f1 = 1, f2 = 2;
swap_double(f1,f2);

--
Clark S. Cox III
cl*******@gmail.com
Feb 22 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.