Home c++ pointer to the pointer - what is it?

pointer to the pointer – what is it?

Author

Date

Category

often meet this design:

int ** p;

I understand that this is a pointer to the pointer. Why is such a duality need and where is used?


Answer 1, Authority 100%

Pointer in C is not semantics, but a mechanism. He does not make sense in itself, but can use to express one or another sense. The same applies to a double pointer: it can be used for different things. Here are some examples.

  1. In C Parameters are transmitted by value, that is, there is no parameter transmission from the box (they are & amp; C++ parameters, they are Ref – / Out Parameters C #). In order to declare such a parameter, use the pointer to the actual parameter (that is, the parameter address is transmitted to the function). If the type of parameter itself is a pointer, a double pointer is obtained. Example:

    void split_half (char * input, char ** second_half, char ** second_half)
    {
      VAR LEN = STRLEN (INPUT);
      var halflen = len / 2;
      * first_half = malloc (Halflen + 1);
      STRNCPY (* First_half, Input, Halflen);
      (* FirstHalf) [Halflen] = 0;
      * Second_Half = Strdup (& amp; Input [Halflen]);
    }
    
  2. in c The pointer may indicate an array. If the type of array element is a pointer, a double pointer is obtained. Classic example:

    int main (int argc, char ** argv) {...}
    
  3. Dual pointer can be used for array of arrays. For example, a square matrix:

    struct matrix
    {
      INT ** DATA;
      INT WIDTH;
      int height;
    }
    Void Init_matrix (Int Width, Int Height, Struct Matrix * Matrix)
    {
      Matrix- & gt; width = width;
      Matrix- & gt; Height = Height;
      Matrix- & gt; Data = Malloc (Height * Sizeof (int *));
      for (int y = 0; y & lt; height; y ++)
        Matrix- & gt; Data [Y] = Malloc (Width * SizeOF (int));
    }
    

in C++ Usually manual memory management is not welcome, so there are multiple pointers going much less frequently.


Answer 2, Authority 33%

None in C++, nor in the C language, there is no such thing as “pointer to a pointer” in the form of independent entity with some new quality properties. Therefore, in the strict sense of the word, the question of “why a pointer is needed to the pointer” does not make any sense.

In C and C++ languages, there is a concept as pointer. Just pointer. Those. If you have a type of T , you can declare p on this type

t * p;

and force this pointer to point to the object T type t

t t;
P = & amp; t;

After this expression T and * P will be denoted by the same object. Those. If you, for example, change the value of the * P object, then you will change the T object (and vice versa). You can also start any other indicators to one and from the same object.

This is an elementary basis of the idea pointer .

Well, then then you can simply notice that the type T itself can be an index type. But this completely does not change anything. There is nothing fundamentally different between the situation when T is int , and the situation when t is double * . All of the above refers to both cases to the same extent.

Here, in fact, everything. Those. There is no point in entering into consideration such an essence as “pointer to the pointer”, and arrange some discussions around it. All that we need is an ordinary pointer that can simply indicate on another pointer. But these two levels of sounds (three, four, five levels …) are completely separate, they do not know anything about each other and do not want to know.

and consider such pointers needed as conventional pointers. The same is fully true about “pointers to pointers to pointers”, “pointers to pointers to pointers to pointers”, etc. to infinity.


Answer 3, Authority 17%

once explained this on the example of the refrigerator.

int ** fridge; // Refrigerator with shelves on which food
* fridge; // Shelf in the refrigerator
** fridge; // Sausage

Answer 4

This design is needed to create a dynamic array, and to not copy all data again, only their index index indexes are copied and followed by filling it.

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions