How to define an array of const pointers in c++?

  • A+

Is there a way to define an array of pointers so that any pointer is const?

e.g can a char** array be defined so array[0] is const and array[1] is const etc. but array is non-const and array[j][i] is non-const?


char* const * pointer;. then

pointer       -> non-const pointer to const pointer to non-const char (char* const *) pointer[0]    -> const pointer to non-const char (char* const) pointer[0][0] -> non-const char 

If you want an array then char* const array[42] = { ... };.

If you don't know the size of array at compile-time and have to allocate the array at run-time, you could use the pointer then

int n = ...; char* const * pointer = new char* const [n] { ... }; ... delete[] pointer; 

As you can see, you have to perform allocation and deallocation manually. Even you've said you don't want std::vector but for mordern C++ using std::vector<char* const> is more appropriate.


:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: