Pointer — variable storing a memory address. Reference — alias for another variable.
Pointer:
1int x = 10;2int* ptr = &x; // Address of x34*ptr = 20; // Change x via pointer5std::cout << x; // 2067// Null pointer8int* null_ptr = nullptr;910// Pointer arithmetic11int arr[] = {1, 2, 3};12int* p = arr; // Points to arr[0]13std::cout << *(p + 1); // 2
Reference:
1int x = 10;2int& ref = x; // Reference to x34ref = 20; // Change x via reference5std::cout << x; // 2067// Reference must be initialized8// int& ref2; // Error!
Key differences: