Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
453 views
in Technique[技术] by (71.8m points)

c - Why is int x[n] wrong where n is a const value?

I cannot understand why doing this is wrong:

const int n = 5; 
int x[n] = { 1,1,3,4,5 };

even though n is already a const value.

While doing this seems to be right for the GNU compiler:

const int n = 5;
int x[n]; /*without initialization*/

I'm aware of VLA feature of C99 and I think it's related to what's going on but I just need some clarification of what's happening in the background.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The key thing to remember is that const and "constant" mean two quite different things.

The const keyword really means "read-only". A constant is a numeric literal, such as 42 or 1.5 (or an enumeration or character constant). A constant expression is a particular kind of expression that can be evaluated at compile time, such as 2 + 2.

So given a declaration:

const int n = 5;

the expression n refers to the value of the object, and it's not treated as a constant expression. A typical compiler will optimize a reference to n, replacing it by the same code it would use for a literal 5, but that's not required -- and the rules for whether an expression is constant are determined by the language, not by the cleverness of the current compiler.

An example of the difference between const (read-only) and constant (evaluated at compile time) is:

const size_t now = time(NULL);

The const keyword means you're not allowed to modify the value of now after its initialization, but the value of time(NULL) clearly cannot be computed until run time.

So this:

const int n = 5;
int x[n];

is no more valid in C than it would be without the const keyword.

The language could (and IMHO probably should) evaluate n as a constant expression; it just isn't defined that way. (C++ does have such a rule; see the C++ standard or a decent reference for the gory details.)

If you want a named constant with the value 5, the most common way is to define a macro:

#define N 5
int x[N];

Another approach is to define an enumeration constant:

enum { n = 5 };
int x[n];

Enumeration constants are constant expressions, and are always of type int (which means this method won't work for types other than int). And it's arguably an abuse of the enum mechanism.

Starting with the 1999 standard, an array can be defined with a non-constant size; this is a VLA, or variable-length array. Such arrays are permitted only at block scope, and may not have initializers (since the compiler is unable to check that the initializer has the correct number of elements).

But given your original code:

const int n = 5; 
int x[n] = { 1,1,3,4,5 };

you can let the compiler infer the length from the initializer:

int x[] = { 1,1,3,4,5 };

And you can then compute the length from the array's size:

const int x_len = sizeof x / sizeof x[0];

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...