Khairul Basar Rofi Khairul Basar Rofi - 7 months ago 24
C Question

why swapping value using pointer doesn't work?

I have written the following code. It should swap the values of two variables ... but, as soon as I compile the code it shows swap.exe has stopped working...

Why doesn't it work?

using namespace std;
void swap(int *x, int *y)
int *temp;
int main()
int i=5,j=10;
cout<<i<<" "<<j<<endl;

return 0;

How to fix this problem?
What's wrong with my code?


Change to

void swap(int *x, int *y)
    int temp;

If you want to swap two ints why do you use a pointer to int as temporary variable? Use the same type.