5 thoughts on “C++ program to swap two numbers using pointers”

  1. Here is how I rewrote:

    //this program will swap two ints using ptrs


    using namespace std;

    int main()
    int* a = new int;
    int* b = new int;

    cout << "Enter value of a and b:";
    cin >> *a >> *b;

    //put a in temp
    int* temp = a;
    //put b in a
    a = b;
    //put temp, was a, into b
    b = temp;

    cout << "nAfter swapingna=" << *a << "nb=" << *b;

    delete a;
    delete b;

    return 0;

    1. based on what i know,
      int* a = new int
      – * a means, a is now a pointer pointing at new int.
      – new int is to set a side an empty memory space to store input from the user. this is from dynamic memory allocation

Leave a Comment

Your email address will not be published. Required fields are marked *