Simple swap function not swapping

I got it in my head to start writing some simple sorting algorithms a little while ago and decided that it would make my life easier to also write a swap function as well.

The problem is that while my swap algorithm seems to work just fine in the sort function, when I put it into it's own function it simply doesn't work. Here's the code: http://codepad.org/dxuC9vLE
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
using namespace std;

void myswap(int left, int right);

int main() {
	int a = 1;
	int b = 2;

	cout << "a:" << a << ", b: " << b << endl;

	myswap(a, b);

	cout << "a:" << a << ", b: " << b;

	return 0;
}

void myswap(int left, int right)
{
	int storeleft = left;
	int * sortpointer;
	sortpointer = &left;
	*sortpointer = right;
	sortpointer = &right;
	*sortpointer = storeleft;
}


When I run it through the debugger it looks like the values are being changed in a and b correctly but once the function exits the values go right back to their original declaration. Any ideas?
Try declaring the function as:

void myswap(int &left, int &right)

to use address reference (default in fortran) instead of value copying (default in C++) in the function, I guess that is all what you need.
Last edited on
Thanks chucthanh, that solved it!
Topic archived. No new replies allowed.