Avatar ·

What are the differences between a pointer and a reference in C++?

Greetings, colleagues! I’ve just started learning C++ after Java and Python, and I’m confused about pointers and references. They both seem to allow working with objects without copying, but I don’t understand when to use which. Here’s an example from my code. I wrote a function that swaps two variables. First, I tried using references: ```cpp void swap_ref(int &a, int &b) { int temp = a; a = b; b = temp; } ``` Then I learned that the same can be done with pointers: ```cpp void swap_ptr(int *a, int *b) { int temp = *a; *a = *b; *b = temp; } ``` Both options work. But I noticed that with pointers, you need to put asterisks and ampersands everywhere when calling, while with references, you don’t. I also heard that references cannot