Cannot implicitly convert type 'double[]' to 'double*[]'

I have created an array "pData" of length 5.  The array is to hold 5 pointers to arrays of doubles:

          double*[] pData;
          pData = new double*[5];

I have created an array "data" of length 10 to hold 10 doubles:
          double[] data;
          data = new double[10];

I populate "data" here:
          for (int i = 0; i < 10; i++)
                    data[i] = -1.0;

I get the error "Cannot implicitly convert type 'double[]' to 'double*[]'"

None of these work:
                              //None of these work:
                                    pData[0] = data;
                                    pData[0] = &(data[0]);
                                    pData[0] = (double*)data;

Thank you, in advance, for your help.
LVL 1
richtelieu88Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ZoppoCommented:
Hi richtelieu88,

IMO it's strange it compiles at all, this is not a valid statement (at least no in VS 2015):
double*[] pData

Open in new window

Another problem is the way you use [ ]: defining a C-style array with [] can be done in two ways, in both cases the size is hardcoded, i.e.:
int a[5]; // uninitilized array for 5 ints
int b = { 1, 2, 3 }; // initialized array for 3 ints

Open in new window

The solution is easy: just replace each [ ] with a *

Next problem is you don't create the 5 mentioned arrays, you only instantiate the 5 pointers for these arrays. For this you need to know the size of each of the 5 arrays. Here's a simplified sample which assumes, all arrays have a size of 10:
const int num = 5; // it's better to hardcode it only once
const int size = 10;

pData = new double*[num];

for ( int n = 0; n < num; n++ )
{
 pData[n] = new double[size];
}

// do whatever you want to do with the array here
// i.e. fill in some values like this:
for ( int n = 0; n < num; n++ )
{
 for ( int m = 0; m < size; m++ )
 {
  pData[n][m] = n * m;
 }
}

// if you don't need it anymore you should clean up (of course it would be better to use some smart-pointers instead)
for ( int n = 0; n < num; n++ )
{
 delete [] pData[n];
}

delete [] pData;

Open in new window

(code is not tested, if you encounter problems please tell)

Hope this helps,

ZOPPO
Kyle AbrahamsSenior .Net DeveloperCommented:
  pData[0] = &data;

Open in new window



you're storing a pointer  to the address of the array.  

[0] means to de-reference it which  would be the address of the first cell.
richtelieu88Author Commented:
ZOPPO:
Program.cs
Become a Microsoft Certified Solutions Expert

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

richtelieu88Author Commented:
ZOPPO:  I failed to mention:

          I'm using C#
          I'm using "unsafe"

I tried to upload my source code about 5 times by editing the question, but Experts Exchange kept preventing me an error message . . .something about a missing tag . . . . . .

Anyway I've uploaded my source code now:  "Program.cs"
richtelieu88Author Commented:
I'm NOT using "C" or "C++".
richtelieu88Author Commented:
Kyle Abrahams:  >>[0] means to de-reference it which  would be the address of the first cell.

That's exactly what I want to do.  The value of "data" is the same as the address of "data[0]".  So "data" should be identical to "&(data[0])"   (in "C" anyway, which this is NOT  .. . .this is C#):
richtelieu88Author Commented:
ZOPPO:  >>Next problem is you don't create the 5 mentioned arrays,

Well, if I could get past the compile error encountered in attempting to populate the 1st element of "pData", that is "pData[0]" then I'd be well on my way to populating the other 5.  I just can't get past this compile error.
Kyle AbrahamsSenior .Net DeveloperCommented:
try the following:
    unsafe static void Main(string[] args)
        {

         

                double*[] pData;
                pData = new double*[5];


                double[] data;
                data = new double[10];

                for (int i = 0; i < 10; i++)
                    data[i] = -1.0;

                fixed(double* ptr = data)
                {
                    pData[0] = ptr;
                }
     
               //sanity check
                *pData[0] = 5;   //data[0] = 5;

                

        }

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Fernando SotoRetiredCommented:
Does this help any?
double[] data;
data = new double[10];
double test = 0.0;

for (int i = 0; i < 10; i++)
{
    data[i] = 1.0 + test;
    test += 1.0;
}

unsafe
{
    fixed (double* fixData = &data[0])
    {
        double*[] pData;
        pData = new double*[5];
        pData[0] = fixData;
        pData[1] = fixData + 1;
        pData[2] = fixData + 2;
        pData[3] = fixData + 3;
        pData[4] = fixData + 4;
        for(int i = 0; i < 5; i++)
        {
            Console.WriteLine("pData[{0}] = {1}", i, *pData[i]);  
        }       
    }
}

Open in new window

See unsafe (C# Reference), Pointer types (C# Programming Guide) and fixed Statement (C# Reference)
ZoppoCommented:
Sorry, C# is not my profession ...
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.