1

Are there any differences between these two pointer declarations to pass a std::vector to a function that has a special signature that I don't really understand?

libraryFunction (int numSamples, double* const* arrayOfChannels) { 
  //things
}

std::vector<double> theVectorA = {11, 22, 33, 44};
double * p_VecA[1];
p_VecA[0] = theVectorA.data();
libraryFunction(theVectorA.size(), p_VecA);

std::vector<double> theVectorB = {55, 66, 77};
double * p_VecB = theVectorB.data();
libraryFunction(theVectorB.size(), p_VecB);

What are the differences between p_VecA and p_VecB?

Can you explain the function signature? I don't understand the last part.

pablo_worker
  • 1,042
  • 9
  • 26

2 Answers2

3

double * p_VecA[1]; creates an array of 1 pointer element, which points to a double (in this case, the first double in theVectorA). Therefore p_VecA is an array of pointers to doubles, in this context if you use the name without index it decays to a pointer to its first element (think of it as double**) and p_VecA[0] is of type double* (like p_VecB is).

double * p_VecB creates a pointer to a double (in this case, the first double in theVectorB).

Update:

Maybe this can help you to understand the signature of libraryFunction():

What is the difference between const int*, const int * const, and int const *?

Like Jack wrote: arrayOfChannels is a pointer to a const pointer to double

Community
  • 1
  • 1
Stefan Scheller
  • 953
  • 1
  • 12
  • 22
1

p_vecA is an array of pointers with size 1

double * p_VecA[1];

p_VecB is pointer

double * p_VecB = theVectorB.data();

Can be written as

double * p_VecB;
p_VecB = theVectorB.data();
Melebius
  • 6,183
  • 4
  • 39
  • 52
Vu Gia Truong
  • 1,022
  • 6
  • 14
  • It's not homework. I have to work with that function but I don't understand the function signature: libraryFunction(int numSamples, double * const * arrayOfChannels). I wrote the first code (p_VecA two years ago and I don't remember why I did it like this, I was thinking that p_VecA and p_VecB were the same, my bad – pablo_worker Dec 07 '17 at 08:26
  • Ha ha everyone made mistakes. Good luck with your wokr – Vu Gia Truong Dec 07 '17 at 08:28