简体   繁体   中英

C++ Pointer to const array of const pointers

I've created a const array of const pointers like so:

const char* const sessionList[] = {
    dataTable0,
    dataTable1,
    dataTable2,
    dataTable3
};

What is the correct syntax for a regular non-const pointer to this array? I thought it would be const char** , but the compiler thinks otherwise.

如果您确实需要一个指向数组的指针,正如标题所示,那么这就是语法:

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 .
Hence, type of &sessionList[0] is 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 . If you want to declare a pointer to the entire array, it needs to be:

char const* const (*ptr)[4] = &sessionList; 

与为数组元素声明的类型相同,添加了额外的*

const char* const *

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