Doubts about pointer arrays

2

I have a doubt between the behavior of two arrays declarations. And it is not related to the dynamic allocation of memory, but how to access the objects of the array. I explain.

Case a:

Suppose I define an array in the following way.

 Time t1(12,13,15);//los parametros del constructor son hora, minuto y segundo.
 Time t2(17,15,18);
 Time *pTArr[2]; //no se hace llamada al constructor.
 pTArr[0]=&t1;
 pTArr[1]=&t2;
 pTArr[1]->print();//imprime hora:minuto:segundo.

In this case pTArr [i], is a pointer where:

  • pTArr[i] is the address where the object i is pointing to, and
  • *pTArr[i] , is the object it points to.

Typical operation of a pointer.

Case b:

  Time *pTArr=new Time[2]; // Se inicializa y se llama al construtor Time();

  pTArr[0].print(); //cuando antes era  pTArr[1]->print().

From what I see, in this case it would be something like this:

  • pTArr[i] is the object it points to.
  • &pTArr[i] is the address where the i object is pointing to.

Why do they behave differently, are not both an array of pointers to objects Time ? In case a, it is the typical behavior of pointers. But the case b, do not behave like a pointer ( puntero=dirección , *puntero=objeto ).

Any clarification, ask. Greetings, Julio.

    
asked by Jcpardo 30.04.2018 в 12:49
source

1 answer

3

You are mixing, in a dangerous way, different concepts.

  • Case a : The declaration Time *pTArr[2] is creating a 1 formation of two elements whose type is "Pointer to Time ".
  • Case b : The declaration Time *pTArr=new Time[2] is creating a "Pointer to Time " pointing to a dynamic memory space containing two objects Time .
  

Why do they behave differently, are not both an array of pointers to objects Time ?

The difference is subtle and I understand that it can be confusing, but in the first case the formation of two elements contains "Pointers to Time ", in the second case you do not have a formation but a pointer pointing to two objects Time contiguous.

Since it is not true that both are formations of pointers to objects Time , it is normal that they behave differently. If you want the case a to behave like the case b , your code should be:

Caso a
Time t1(12, 13, 15); // Los parámetros del constructor son hora, minuto y segundo.
Time t2(17, 15, 18);
Time pTArr[2]; // Se llama al constructor por defecto Time().
//  ~ <--- Sin especificar "puntero a Time".
pTArr[0] = t1;
//        ~ <--- Sin pedir la dirección de 't1', se llama al operador de copia.
pTArr[1] = t2;
//        ~ <--- Sin pedir la dirección de 't2', se llama al operador de copia.
pTArr[1].print(); // Imprime hora:minuto:segundo.
//      ~ <--- Punto, no flecha.
Caso b
Time *pTArr=new Time[2];
pTArr[0].print();

If you want the case b to behave like the case a , your code should be:

Caso a
Time t1(12,13,15); // Los parámetros del constructor son hora, minuto y segundo.
Time t2(17,15,18);
Time *pTArr[2]; // No se hace llamada al constructor.
pTArr[0] = &t1;
pTArr[1] = &t2;
pTArr[1]->print(); // Imprime hora:minuto:segundo.
Caso b
Time **pTArr=new Time*[2];
//   ^^              ~ <--- formación de punteros
//   || <--- doble puntero
pTArr[0]->print(); // Esto fallará porque no hay nada en pTArr[0].
  • Also known as array or English array.
  • answered by 30.04.2018 / 13:10
    source