1

For example I have:

int boo[8];
boo[1] = boo[3] = boo[7] = 4;
boo[0] = boo[2] = 7;
boo[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?

Templar
  • 1,843
  • 7
  • 29
  • 42
  • 1
    If by "constant values" you mean an initialization list, you can't. But some compilers provide interesting extensions. Take a look this question http://stackoverflow.com/questions/201101/how-to-initialize-an-array-in-c – Alexei Sholik Apr 07 '11 at 13:47
  • Do you mean you want const int boo[8] ? – Dinaiz Apr 07 '11 at 13:47

2 Answers2

7

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];
Al Riddoch
  • 583
  • 2
  • 9
3

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_;
BenjaminB
  • 1,809
  • 3
  • 18
  • 32
  • 1
    Nah, it seems quite elegant to me :) – x10 Apr 07 '11 at 13:54
  • Use `boo_` instead of `_boo`. Names starting with underscores, or double underscores, are reserved for use by the implementation(compiler). There are certain rules here but its probably just best to avoid the underscore at the beginning all together. – edA-qa mort-ora-y Apr 07 '11 at 14:03