nunojpg
nunojpg

Reputation: 505

C++ read-only array literal

The possibility to create a array literal on read-only memory, exists as the string literal, but doesn't look to extend to other types.

const char* const kChar1{"This is a name"};
const char kChar2[]={"This is a name"};

const int* const kInt1{5,3,2,6,9,0,0,2};  //error
const int kInt2[]{5,3,2,6,9,0,0,2};

I can't create KInt1, like I created kChar1.

How could I create the equivalent?

Upvotes: 2

Views: 2238

Answers (2)

wshcdr
wshcdr

Reputation: 967

I think this is correct

const int one = 5;
const int two = 10;
const int* const kInt1[] ={&one,&two};

Upvotes: -1

Vaughn Cato
Vaughn Cato

Reputation: 64308

This is fairly close:

const int kInt2[]{5,3,2,6,9,0,0,2};
const int* const kInt1 = kInt2;

The only real difference is that kInt1 will necessarily point to the same memory as kInt2, but kChar1 does not necessarily point to the same memory as kChar2.

Upvotes: 4

Related Questions