Khairul Basar Rofi Khairul Basar Rofi - 1 year ago 62
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?

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download