Double* modification for input

I created a 1d array using the trick double* x = new double[var]; which seems to be the only way of creating an array with a variable size. However, I now need to use the parts of x --> (x[1], x[2], ..., x[i]) in a function that requires a double input, and I cannot do that with a double*.

Is there a way of solving this without entering the source code and trying to change the input method, or rather, is there a way of turning a double* to a double, or better yet, create a double with a variable input?

Thanks in advance.
When a function takes a double, you can only give it a single double, not more than one at once.
For examples:
1
2
3
4
x[0] // is the first element into x
x[1] // is the second ...
x[var-1] // is the last
x[var] // FAILURE! Don't Do This! 
Thank you, that is accurate, but not what I was asking. I know I formulated the question poorly, sorry about that.

What I meant to ask is: when I attempt to make the size of a double[] using a variable, the error:
expression must have a constant value
appears. The way to bypass this is to make the double[] into a double*[]. However, this type is not compatible with a function I want to run it through.

So my question is: do I have to rewrite the function, or is there a way of bypassing this error?
Sorry but, Have you tried passing it normally?
Let me explain you this:

1
2
3
4
5
6
void FunctionThatTakesDoubleArray(double[] Data) {}
// ...
double StaticSized[5];
double * DynamicSized = new double[5];
FunctionThatTakesDoubleArray(StaticSized); // Valid
FunctionThatTakesDoubleArray(DynamicSized); // Also Valid, Same Effect 


If yet I didn't answer correctly, could you make an example, posting a little code snip maybe?
Last edited on
is there a way of turning a double* to a double

Dereference it
1
2
3
4
double* x = new double[var];
// Initialize array elements
double d = *x; // It's the same as doing d = x[0]
double d2 = *(x + 2); // Same as x[2] 


create a double with a variable input?

Not sure what you're asking here

However, I now need to use the parts of x --> (x[1], x[2], ..., x[i]) in a function that requires a double input, and I cannot do that with a double*

If your function works with a single double at a time, just pass it using an index
1
2
3
4
 void function(double);

// In main()
function(x[0]);


I'm guessing you just started dealing with pointers. If you need help understanding what's going on feel free to ask
Topic archived. No new replies allowed.