Neil
Neil

Reputation: 249

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.

Upvotes: 7

Views: 6234

Answers (3)

R Sahu
R Sahu

Reputation: 206577

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; 

Upvotes: 5

juanchopanza
juanchopanza

Reputation: 227400

If you actually need a pointer to an array, as your title suggests, then this is the syntax:

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

Upvotes: 6

Chris Dodd
Chris Dodd

Reputation: 126203

The same type as you declared for the array elements, with an extra * added:

const char* const *

Upvotes: 2

Related Questions