How do you initialize the value of x using array new:
int (*x)[5] = ?
new int[5]
doesn't work because it has a type of int*
Do you have to use a C-style cast as follows?
int (*x)[5] = (int( *)[5])(new int[5]);
How do you initialize the value of x using array new:
int (*x)[5] = ?
new int[5]
doesn't work because it has a type of int*
Do you have to use a C-style cast as follows?
int (*x)[5] = (int( *)[5])(new int[5]);
A fixed sized array in C++ would be std::array
.
And, guessing from the pointer to array int (*x)[5]
this might be either
std::array<int, 5> x;
or
std::array<int, 5> *x = new std::array<int, 5>;
Although the former is preferred, unless there's a real need to put the array on the heap.
If you want a variable number of fixed sized arrays, combine this with a std::vector
std::vector<std::array<int, 5> > x;
This works
typedef int int5[5];
int main()
{
int (*x)[5] = new int5[1];
return 0;
}
There may be a way to do it without a typedef, but I didn't investigate too much.
UPDATE
Somewhat counter intuitively this is also correct
int (*x)[5] = new int[1][5];
And no you shouldn't use a cast, casts are rarely the right solution, especially for beginners.