-1

Can someone be able to tell me how to convert vector to an array? I have a function that returns a vector of double type with typical like this

std::vector<double>  x_vec = LinspaceArray_DK2(0, 5.0, 0.01);
std::vector<double>  y_vec = Calculate_y_vec(x_vec);

However, following GSL (GNU scientific lib) function is expecting an array as an input

gsl_spline_init (spline, x_array, y_array, x_vec.length);

Here, x_array, y_array are arrays corresponding to vectors x_vec and y_vec.

I tried following solution here (How to convert vector to array in C++) which suggest conversion like this:

std::vector<double> v;
double* a = &v[0];

But, this type of conversion did not help with gsl_spline_init which expected arrays as inputs.

Updated:

I tried to call like this:

// Convert all vector to an array
double* x_input_Array = &x_input[0];
double* y_input_Array = &y_input[0];

and then call:

gsl_spline_init (spline, *x_input_Array, *y_input_Array, iNoOfPtsIni);

I am getting this error: error C2664: 'gsl_spline_init' : cannot convert parameter 2 from 'double' to 'const double []'

Thanks, DK

Community
  • 1
  • 1
Garima Singh
  • 1,410
  • 5
  • 22
  • 46

1 Answers1

3

You do not need (and should not) dereference - the function you call expects arrays, and you pass their first elements. Use

gsl_spline_init (spline, x_input_Array, y_input_Array, iNoOfPtsIni);
Wojtek Surowka
  • 20,535
  • 4
  • 44
  • 51
  • Thanks @WojtekSurowka : it worked. However, I am confused about pointer to arrays. As gsl_spline_init needs arrays as input; how inputing pointer works out fine? Do you know any online resources for clearing my doubts about arrays? – Garima Singh Aug 15 '14 at 12:22
  • 1
    @DushyantKumar The function doesn't want *arrays* (even it the arguments are named that way), it wants *pointers*. Always remember that arrays, when passed as arguments to functions, decays to pointers. So even if an argument is declared with array-like syntax (like `double x_array[]`) the argument is still a pointer. – Some programmer dude Aug 15 '14 at 12:27
  • @DushyantKumar Try e.g. http://en.wikibooks.org/wiki/C_Programming/Pointers_and_arrays#Pointers_and_Arrays – Wojtek Surowka Aug 15 '14 at 12:30
  • @JoachimPileborg : Thanks for explaining to me. One more question. How does similar conversion works for function with input as reference? Can I still pass pointer? – Garima Singh Aug 15 '14 at 12:32