[英]C++ Pointer to const array of const pointers
I've created a const array of const pointers like so: 我已经创建了const指针的const数组,如下所示:
const char* const sessionList[] = {
dataTable0,
dataTable1,
dataTable2,
dataTable3
};
What is the correct syntax for a regular non-const pointer to this array? 这个数组的常规非const指针的正确语法是什么? I thought it would be const char**
, but the compiler thinks otherwise. 我认为它将是const char**
,但编译器认为不然。
如果您确实需要一个指向数组的指针,正如标题所示,那么这就是语法:
const char* const (*ptr)[4] = &sessionList;
const char* const sessionList[] = { ... };
is better written as: 写得更好:
char const* const sessionList[] = { ... };
Type of sessionList[0]
is char const* const
. sessionList[0]
类型是char const* const
。
Hence, type of &sessionList[0]
is char const* const*
. 因此, &sessionList[0]
是char const* const*
。
You can use: 您可以使用:
char const* const* ptr = &sessionList[0];
or 要么
char const* const* ptr = sessionList;
That declares a pointer to the elements of sessionList
. 这声明了一个指向sessionList
元素的sessionList
。 If you want to declare a pointer to the entire array, it needs to be: 如果要声明指向整个数组的指针,则需要:
char const* const (*ptr)[4] = &sessionList;
与为数组元素声明的类型相同,添加了额外的*
:
const char* const *
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.