RR said:
Hello I am trying to declare in my object something like
double **ma;
then do something like
ma = new int[x][y];
so I just want to declare a pointer to a double array and then
initialize the double array but I keep getting compiler error if
I do the above.
Is there an easy to do this
In the example above, you are neither declaring a pointer to a
double array, nor are you initializing anything with a double
array: you are trying to initialize a
pointer-to-pointer-to-double with an array-of-arrays-of-int. They
have pretty much nothing at all in common, and one certainly
isn't convertable to the other.
Part of your problem above may have just been to accidentally
type "int" where you meant "double"; however, it's still
important to realize that arrays and pointers are still *very*
different types. Don't let anyone try to tell you that arrays are
really pointers--that's what probably led you to this confusion
in the first place.
Even if the above had been:
double **ma = new double[x][y];
It'd still fail to compile. A (double [][]) can't be assigned to
a (double**): you wouldn't even be able to assign a (double [])
to a (double *), where it not the fact that an array expression
can be implicitly converted to a pointer to the first element of
an array. But that implicit conversion applied to a (double [][])
results in a (double (*)[]) [that is, a
pointer-to-array-of-doubles]... which is absolutely *not* the
same as a pointer-to-pointer-to-double.
So go get a good C++ book (Stroustrup, for example), and study
the difference between arrays and pointers until you are certain
you understand the difference.
If y is a constant, you could do:
double (*ma)[y];
followed by (or initialized similarly to):
ma = new double[x][y];
But I have a nagging suspicion that whatever you are doing may
not be the best way to address the problem you are building this
particular solution for. So: what is the context? What are you
trying to accomplish?