views:

228

answers:

3

Hi,

I need to swap a couple of integers in the format int i[2] using a void swap(int& x) function. As you see the function takes an argument of type int&. Here is non-working version of the function:

int i[2] = {3, 7};

void swap (int& x)
{
    int temp;
    temp = x[1];
    x[1] = x[0];
    x[0] = temp;
}

int main()
{
    cout << i[0] << ", " << i[1] << "\n"; // return the original: (3, 7)
    swap(i);
    cout << i[0] << ", " << i[1] << "\n"; // return i swapped: (7, 3)
}

How should I do this?

Edit: The answer CANNOT use anything else for the function parameter. It MUST use a int& parameter. This is a problem from Bjarne Stroustrup's book: "The C++ programming language", third edition. It is problem #4 from chapter 5. The problem first asks to write a function taking a int* as parameter, than to modify it to accept a int& as parameter.

+6  A: 

A reference isn't a pointer. I'd recommend changing the function signature if you can, but if you're stuck with it, you could do something like:

int *xx = &x;
int temp = xx[1];
xx[1] = xx[0];
xx[0] = temp;

That said, you should probably just use std::swap instead.

Jerry Coffin
But, a reference in C++ is actually substituted by corresponding pointer arithmetics by compilers. So, I think technically references and pointers are same except some semantic issues. Correct me if I'm wrong.
minjang
@minjang: Only the behavior of references is defined. How that behavior comes about is an implementation issue. That said, yes, all (?) implementations treat references as pointers underneath.
GMan
@minjang:The same only different. One difference is that pointers support operator[], and references don't. In this case, that's a serious difference.
Jerry Coffin
Upvoted as correct, but it seems like a crazy question. It's safe to take the address of a reference, but it's not safe to then take xx[1]. Who says the next int must exist? Not the function signature, that's for sure.
olooney
I meant implementation level, not syntax and semantic level. Yes, semantics are different.
minjang
+3  A: 

Looking at my copy, the exercise doesn't say how swap() should look like. It just says that it "swaps (exchanges the value of) two integers" and should take a) int* b) int& as the argument type.

As you tagged the question learning, the real question becomes:
Why does your swap() only take one argument?

Georg Fritzsche
It seemed more interesting to me, although I can conceive that swapping two variables is also very interesting. What would make it more interesting to pass two references as arguments instead of one? Would there be a way of still using only one?
Morlock
At least the exercise does not require you to use only one argument, thats not its intent.
Georg Fritzsche
Thanks for the insight.
Morlock
A: 

All right, thanks to @gf's suggestions, I found a solution :) Many thanks! Please tell me if you see anything not very C++ish in there.

// Swap integers

#include<iostream>

using namespace std;

int i = 3;
int j = 7;

void swap (int& x, int& y)
{
    int temp = x;
    x = y;
    y = temp;
}

int main()
{
    cout << i << ", " << j << "\n"; // return the original: (3, 7)
    swap(i, j);
    cout << i << ", " << j << "\n"; // return i swapped: (7, 3)
}
Morlock