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

cin>>*x[index];

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..

Recommended Answers

All 2 Replies

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.

Rather than cin>>*x[index]; You should perhaps try: cin>>(*x)[index]; Precedence of [ ] is higher than * http://ocw.kfupm.edu.sa/user/ICS43153/Reference/C-Reference/C-Prog/section3_16.html
The the statement you wrote in the first post will be interpreted like this:
*(*(x+index))
As you can see, this has got no meaning.
Rather the code I just gave would be interpreted like this:
*((*x)+index)
which definitely have a meaning

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.