开发者

Stating constant arrays value in C++

开发者 https://www.devze.com 2023-02-22 14:50 出处:网络
For example I have: int boo[8]; boo[1] = boo[3] = boo[7] = 4; boo[0] = boo[2] = 7; b开发者_运维问答oo[4] = boo[5] = boo[6] = 15;

For example I have:

int boo[8];
boo[1] = boo[3] = boo[7] = 4;
boo[0] = boo[2] = 7;
b开发者_运维问答oo[4] = boo[5] = boo[6] = 15;

How I should type it as constant values? I saw similar question but it didn't help me.

EDIT: One more question what about if boo with indexes 0 1 3 4 5 6 7 is constant but boo[2] is not? is it possible to do it?


Is this what you are looking for?

const int boo[] = { 7, 4, 7, 4, 15, 15, 15, 4 };

Get a non-const pointer to one entry in the array like this:

int * foo = (int*)&boo[2];


One not so elegant solution may be:

const int boo[8] = {7,4,7,4,15,15,15,4};

Another solution may be:

int boo_[8];
boo_[1] = boo_[3] = boo_[7] = 4;
boo_[0] = boo_[2] = 7;
boo_[4] = boo_[5] = boo_[6] = 15;
const int * boo = boo_;
0

精彩评论

暂无评论...
验证码 换一张
取 消