hi guys...

im having a problem figuring out exactly how to fill a dynamically allocated array of doubles.

bool getData(double **x, double **y, int n)
my function is passed these two pointers to pointers and, in the end, they are supposed to point to the addresses of dynamically allocated arrays of n doubles.

Im at a complete loss of how to do this...

I figured that doing something like:
*x = new double[n];
*y = new double[n];

would create the arrays successfully and theeen in order to access each element of the array, i would be able to do somethjing like


BUT this doesnt work.... I know that normally, it would be easier to probably not have pointers to pointers but i have to adhere to this rule..

You might find it interesting to know that from the main function, my get data function is called like this:

double *xarr = NULL; // Pointer to array of x coordinates
double *yarr = NULL; // Pointer to array of y coordinates

getData(&xarr, &yarr, num);

I can post more code if necessary. Ive tried more than just the non working suggestion i posted here but nothing seems to work..

I don't see a problem with the code you posted. I even tried running the following:-

void getData(double **x, double **y, int n)
    *x = new double[n];
    *y = new double[n];
    *x[0] = 2.6;

And the call:-
double *xarr = NULL;
double *yarr = NULL;
getData(&xarr, &yarr, 3);

All works fine. I didn't try it with cin>>*x[0], though I don't see why that would be a problem.

This article has been dead for over six months. Start a new discussion instead.