I have an API for the functions, say:
void func (const char** s,const size_t* ss);
I.e. the function gets the const array of null terminated strings [s] and their sizes [ss].
I get the strings during run-time and not aware about their amount.
What I thought to do is to define vector<char*> vS – insert the strings to it and define vector<size_t> vSS – insert the strings sizes to it.
Eventually I should transfer vector<char*> vS to const char**
and vector<size_t> vSS to const size_t*.
I am aware that exists a trick &vS[0] / &vSS[0].The problem is that the above generates char** and size_t* ss respectively.But I am missing const.
How the issue could be solved?
If you are passing params to function (and not trying to pass results back in the arguments), than non-const’ness is not a problem: you may use
char *anywhere wherecont char *is required.But you have problems with double pointers –
const char **, etc. They are not converted implicitly because it can lead to const violation. You can read an explanation in C++ FAQ Light.In your case you can just create a
vector<const char*>, as @aschepler said.BTW,
const char**is not “const pointer to pointer to char”, but “pointer to const pointer to char”, you have to add anotherconstif you want to ensure that function doesn’t change the outer pointer contents.const char * const *orchar const * const *, which is the same – pointer to const pointer to const characters.(BTW, reading right-to-left relly helps when dealing with multiple consts: try it with the last expression from the previous paragraph. And yes, you can add another
constat the end of this exprssion to get “const pointer to const pointer to const char”).