constants in array size

G

Gopi Sundaram

I have the following code:

const int num_segments = 16;

int some_function(void)
{
int key[num_segments + 2];

...
}

My compiler barfs out the warning "ANSI C forbids variable-size array",
but lets it pass.

Does an expression using a const-variable count for the purposes of
"variable-size array"? I was under the impression that declaration is
legal ANSI C.
 
P

Peter Hickman

Gopi said:
I have the following code:

const int num_segments = 16;

int some_function(void)
{
int key[num_segments + 2];

...
}

My compiler barfs out the warning "ANSI C forbids variable-size array",
but lets it pass.

Does an expression using a const-variable count for the purposes of
"variable-size array"? I was under the impression that declaration is
legal ANSI C.

switch will also complain if you use const ints to match against ints. This is
really annoying.
 
E

Eric Sosman

Peter said:
Gopi said:
I have the following code:

const int num_segments = 16;

int some_function(void)
{
int key[num_segments + 2];

...
}

My compiler barfs out the warning "ANSI C forbids variable-size array",
but lets it pass.

Does an expression using a const-variable count for the purposes of
"variable-size array"? I was under the impression that declaration is
legal ANSI C.

`const' does not mean "constant." What it means is
something more like "read-only." `num_segments' is not
a constant, but a variable -- a read-only variable, but
a variable nonetheless.
switch will also complain if you use const ints to match against ints. This is
really annoying.

Same reason.

Non-constant `const' looks pretty silly in contexts
like the above, but consider this one:

extern const int num_segments;
...
int key[num_segments];

As before, `num_segments' is a read-only variable, but
it is defined in some other module. The compiler cannot
"see" that other module while compiling this one, so it
cannot know what the variable's value might be. In fact,
there might be seven different definitions of `num_segments'
in seven different source files; the compiler has no way
of knowing which of those sources will eventually be linked
into the same program with this one.
 
J

Jack Klein

I have the following code:

const int num_segments = 16;

int some_function(void)
{
int key[num_segments + 2];

...
}

My compiler barfs out the warning "ANSI C forbids variable-size array",
but lets it pass.

Does an expression using a const-variable count for the purposes of
"variable-size array"? I was under the impression that declaration is
legal ANSI C.

The declaration is legal in a compiler that conforms with the updated
1999 version of the C language standard, although such compilers are
rather few and far between, and the conformance of most of them is not
complete. The error message you cite seems to indicate that your
compiler does not conform to C99, or is not being invoked in a C99
conforming mode, but rather accepts this declaration as a non-standard
extension of its own.

Under C99 automatic arrays, those defined inside functions without use
of the 'static' keyword, can have variable sizes so long as the size
is greater than 0.

It is illegal under the original ANSI 1989 and ISO 1990 standards.

Prior to C99, and for arrays with static storage duration even under
C99, the size of an array must be specified as a compile-time constant
expression. The value of an object, even a const qualified object, is
not a compile-time constant expression under any version of C.
 
F

Flash Gordon

Peter said:
Gopi said:
I have the following code:

const int num_segments = 16;

int some_function(void)
{
int key[num_segments + 2];

...
}

My compiler barfs out the warning "ANSI C forbids variable-size
array", but lets it pass.

Does an expression using a const-variable count for the purposes of
"variable-size array"? I was under the impression that declaration
is legal ANSI C.

`const' does not mean "constant." What it means is
something more like "read-only." `num_segments' is not
a constant, but a variable -- a read-only variable, but
a variable nonetheless.

<snip>

The OP can get what he probably wants by using:

enum { NUM_SEGMENTS = 16 };

Unlike a #define, the enum follows the same scoping rules as variables.
I.e. if you do it inside a function it will only exist inside the
function.

On the other hand, if you do
#define NUM_SEGMENTS 16

then NUM_SEGMENTS will exist from that line until the end of the file or
you do something explicit to change it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,155
Messages
2,570,871
Members
47,401
Latest member
CliffGrime

Latest Threads

Top