Compile errors when trying to create the classic 'swap ints' program

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)

Thanks!
In swap() add two asterisk near pointers:

1
2
3
4
5
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}


There is no difference between int *a and int* a or int * a.
Awesome thanks for the quick reply tfityo!
Topic archived. No new replies allowed.