简体   繁体   中英

Pointer to std::vector, pointer declaration

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.

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

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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM