This one:
int static_Array[5];
int *dynamic_Array = new int[5]; // 1.
dynamic_Array = static_Array; // 2.
- dynamically allocates an array and stores the address of first element in pointer
dynamic_Array
- overwrites this address with an address of the first element of the
static_Array
But this one:
int static_Array[5];
int *dynamic_Array = new int[5];
static_Array = dynamic_Array;
attempts to initialize an array using an address (thus compiler gives error about invalid pointer to array conversion).
So why the first one works?
Because static array (on the right side) immediately decays to a pointer. This can not work the other way, there is no guarantee that this pointer points to an array.