This question has already been answered:

#include <iostream> using namespace std; void display_ptr(int *ptrint) { cout << *ptrint << endl; } void display_addr(int &toint) { cout << toint << endl; } int main() { int i{ 10 }; display_ptr(&i); display_addr(i); } 

In this example, the possibilities of a construction with a pointer are not clear to me, compared to passing a value by reference.

As you can see, to use a pointer value, you need to get it using * , and also pass in an argument using & .

Using the transmission by reference, nothing is needed except the indication in the parameter itself, while the value is also not copied and can also be changed by reference.

I do not quite understand what profit pointers have if it is much more convenient to pass the value by reference.

Reported as a duplicate by Abyx , aleksandr barakin , D-side , pavel , Mr. Black Aug 3 '16 at 1:02 .

A similar question was asked earlier and an answer has already been received. If the answers provided are not exhaustive, please ask a new question .

  • Well, pass the C - string without a pointer to the function ... First of all, just pointers, links appeared later. - pavel
  • five
    Down: Once, there are no null references in C ++. Two, you cannot create a link container. Three, it is not possible to define a copy assignment operator for a class that has a reference field. Four, the link can not be redirected. - yrHeTaTeJlb

2 answers 2

If English is good, then you are welcome to the English-speaking Stackerfly . If in a nutshell:

  1. Link can not be redirected
  2. Cannot create link to link
  3. Pointer can be reset
  4. Pointers support address arithmetic (+, -, ++, -)
  5. Pointer must be dereferenced
  6. A pointer is a variable that contains a memory address. Regardless of how the link is implemented, the link has the same memory address as the item it refers to.
  7. Links can not be placed in an array
  8. You can create constant references to temporary objects.
  • 3
    "If English is good, then you are welcome to the English-speaking Stackerfly." - this is so possible to answer most of the questions. The point is to create a database of answers here. - Malcolm

The main thing is that the links are always initialized. That is, there are no null links, and broken links are UB. The language considers them as such, although it really does not do anything so that they remain initialized. As always in crosses, half the work falls on the programmer. All other properties of the links are derived from the initialization property.