Increment Value Pointer vs Reference

#include <iostream>
using namespace std;

void incrementPtr(int* x) {
    *x = (*x) + 1;
}

void incrementRef(int& x) {
    x++;
}

int main() {
    int n;
    cin >> n;

    int a = n;
    incrementPtr(&a);
    cout << "After incrementPtr: " << a << "\n";

    int b = n;
    incrementRef(b);
    cout << "After incrementRef: " << b;

    return 0;
}
Upvote
Downvote
Loading...

Input

5

Expected Output

After incrementPtr: 6 After incrementRef: 6