C++ All-in-One For Dummies
Book image
Explore Book Buy On Amazon

If you have an array and you don’t want its contents to change, you can make it a constant array. The following lines of code, found in the Array05 example, demonstrate this approach:

const int Permanent[5] = { 1, 2, 3, 4, 5 };
cout << Permanent[1] << endl;

This array works like any other array, except you cannot change the numbers inside it. If you add a line like the following line, you get a compiler error, because the compiler is aware of constants:

Permanent[2] = 5;

Here’s the error you get when you try this in Code::Blocks:

error: assignment of read-only location 'Permanent[2]'

What about a constant array of nonconstants? Can you do that? Well, sometimes — depending on the compiler. As horrible as the following code (found in the Array06 example) looks — and it’s not ANSI-standard! — you are allowed to do this with older versions of the gcc compilers. (Microsoft Visual C++ and Borland C++ Builder don’t allow it, and the Code::Blocks compiler presents you with an error: invalid array assignment error message.)

int NonConstant[5] = { 1, 2, 3, 4, 5 };
int OtherList[5] = { 10, 11, 12, 13, 14 };
OtherList = NonConstant;

In other words, that third line is saying, “Forget what OtherList points to; instead, make it point to the first array, {1,2,3,4,5}!” Now, you really shouldn't write code like this (remember, keep things simple and understandable), so if you want to prevent this kind of thing, you can make the array constant:

const int NonConstant[5] = { 1, 2, 3, 4, 5 };
const int OtherList[5] = { 10, 11, 12, 13, 14 };
OtherList = NonConstant;

Now, when the compiler gets to the third line, it gives you an error:

error: assignment of read-only variable 'OtherList'

But you may notice that the way you made the array constant was the same way that you made its elements constant in the code that came just before this example. Oops! What’s that all about? Turns out there are some rules.

The following list describes the rules, in detail, for making arrays constant:

  • If you want to make an array constant, you can precede its type with the word const. When you do so, the array name is constant, and the elements inside the array are also constant. Thus you cannot have a constant array with nonconstant elements, nor can you have a nonconstant array with constant elements.

  • The notion of a nonconstant array exists only in gcc and is not ANSI-standard.

If you really want to get technical, the C++ ANSI standard says that when you put the word const in front of an array declaration, you’re not making the array constant; you’re saying that the array holds only constants.

Yet, when you use const this way, most compilers also make the array itself constant. But that’s fine; people shouldn’t be taking an array name and copying it to something else. That’s not good programming style, and it’s just asking for bugs — or, at the very least, confusion — later.

About This Article

This article is from the book:

About the book author:

John Mueller has produced 114 books and more than 600 articles on topics ranging from functional programming techniques to working with Amazon Web Services (AWS). Luca Massaron, a Google Developer Expert (GDE),??interprets big data and transforms it into smart data through simple and effective data mining and machine learning techniques.

This article can be found in the category: