Is there a way to define an array of pointers so that any pointer is const?
For example, 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
or smart pointers is more appropriate.
For such a request you can use the magic tool cdecl
(also available as a web UI here ):
$ cdecl -+ %c++ mode
Type `help' or `?' for help
cdecl> declare x as array of const pointer to char
char * const x[]
cdecl>
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.