An explanation, why are the speakers?

No, I understood the principle of the work, but I didn’t understand one thing - WHAT? Why work with the member functions alternating with the reference if I can work with them indirectly?

  • 2
    If you do not understand why, then you do not understand pointers! - KoVadim
  • See, for example, man qsort (this is a very useful sort function), think about how it can be programmed and much will become clear. - avp
  • qsort is implemented without pointers. For example, in languages ​​like lisp / haskell, where there are no pointers (at least explicitly). On the pluses, you can also do without pointers, but only memory will be consumed more. - KoVadim
  • What is the point to fight for the extra hundred bytes of memory? - Xyanight
  • one
    In fact, everything is much simpler, pointers are needed in order to write "magic" codes, fast and unpleasant. + so that those who do not know C ++ think that C ++ is something incredibly complex: D It is necessary to use x because they are there and your code becomes more incomprehensible. **** PS: It seemed to me - explained at your current level **** PPS: do not comment on the comment, for commenting is trolling itself: D - Zowie

3 answers 3

"If someone lights the stars, then it needs someone"

Here is an example. Suppose you pass a variable to the function. There, with its value, something is being done, and the new value should be entered into the variable whose value you passed to the function. If you do something like this

void func (int a) { a += 10; } ...... int i = 2; func(i); 

That value of the variable i will not change, because a copy of this variable is passed to the function and operations are performed with it. And if you pass a pointer or a link to this variable, you will get access to it and you can change its value.

 void func (int* a) { *a += 10; } ...... int i = 2; func(&i); 

If you need to transfer a structure or object to a function, you can transfer it entirely, but it can take a lot of memory and time, because they can be very large. In simple examples, this is imperceptible, but in real programs, where it can be repeated thousands and millions of times, the brakes will be huge. Instead, pass a pointer or a link and get access to the object from the function.

Already talked about dynamic memory allocation. If you have not read about it yet, then read it. There without pointers IN PRINCIPLE not enough.

These are just a few examples. The deeper you go into C ++, the more you will learn about it.

And further. Do not think that programmers before you were fools.

    Pointers apply:

    1. To return multiple values ​​from a function. As an argument, a pointer to the variable is passed, the function writes the value there. This approach is very common in DirectX, OpenGL, Windows API and other C-style libraries. You can also use links for this, but it is not recommended, since the syntax for sending and returning is indistinguishable.

    2. To store the address of the dynamically allocated memory. It differs from the usual one in that the programmer himself regulates the lifetime of objects, and it is longer (and the stack size is only about a megabyte). If the address is lost, the memory can neither be used nor freed. A memory leak will occur.

    3. C-string is a pointer to its first character.

    4. To create various data structures: linked lists, trees, etc.

    5. To pass an argument to a function without copying (and calling the constructor for objects), which can be long for complex objects. True, it is better to use constant links.

    Thus, there are a lot of pointer applications.

    • one
      @Xyanight, remember that arrays in C are pointers (not even in the case of parameters). For example: int a [10], * b = a + 5; ... printf ("% d \ n", b [0]); // prints the value of a [5] In general, pointers in C allow you to write more generalized code. - avp
    • As an argument, a pointer to the variable is passed, the function writes the value there. - Ilja

    To allocate a large amount of memory. I advise you to read about the stack and heap. A large amount of data on the stack will overflow.

    • That's it! The author of the question needs to look in the direction of dimensional and reference data types, how they are arranged, what are the differences, where they are stored, why they were divided, and so on. Why is there trolling ............ - wind
    • No, I understood the principle of the work, but I didn’t understand it alone - WHAT did Maleh contradict the nonsense? Do not find - Zowie
    • I agree - the author did not understand this topic! - wind
    • one
      @AlexWindHope well, it means that they were badly explained - andrybak
    • one
      public and private assumes that the code can be used by someone else who can climb with his own hands where it is not necessary and break everything. - insolor