Thursday, 13 July 2017

Reference to a Pointer

Notes for reading C++ Primer 4/e.
void ptrswap(int *&v1, int *&v2) {
    int *tmp = v2;
    v1 = v2;
    v2 = tmp;
}

int main() {
    int i = 10;
    int j = 20;
    int *pi = &i;
    int *pj = &j;

    cout << *pi << "  " << *pj << endl;
    ptrswap(pi, pj);
    cout << *pi << " " << *pj << endl;

    return 0;
}
Result:
10 20
20 10

No comments:

Post a comment