I have a program which is supposed to swap the values of two integers, but I am getting compile errors and don't understand why.
Here is the code for swapInts.cpp :
#include <iostream>
using namespace std;
void swap(int *a, int *b) {
int temp = *a;
a = *b;
b = temp;
}
int main () {
cout << "Enter two integers: ";
int x, y;
cin >> x >> y;
swap (&x, &y);
cout << "x = " << x << endl;
cout << "y = " << y << endl;
}
These are the errors I get when I try to compile:
swapInts.cpp: In function void swap(int*, int*):
swapInts.cpp:7: error: invalid conversion from int to int*
swapInts.cpp:8: error: invalid conversion from int to int*
Can you please help clarify where I am going wrong, and what I need to change to fix this?
Also is there any difference between void swap(int *a, int *b) and void swap(int* a, int* b)? (The * is in a slightly different location)