I know that the general technique for creating a dynamic array using new
in C ++ is:
int * arr = new int[5];
The book also says:
short tell[10]; // tell an array of 20 bytes cout << tell << endl; // displays &tell[0] cout << &tell << endl; // displays address of the whole array short (*p)[10] = &tell; // p points to an array of 20 shorts
Now I am wondering if there is a way to allocate memory for the array using new
, so it can then be assigned to a pointer to the entire array. It might look like this ::
int (*p)[5] = new int[5];
The above example does not work. The left side looks right to me. But I do not know what should be on the right.
My intention is to understand if this is possible. And I know that there is std::vector
and std::array
.
UPD
Here is what I really wanted to check:
int (*p1)[5] = (int (*)[5]) new int[5];
And here is how to access these arrays:
memset(*p1, 0, sizeof(*p1)); cout << "p1[0] = " << (*p1)[0] << endl; memset(p2, 0, sizeof(*p2) * 5); cout << "p2[0] = " << p2[0] << endl;
source share