constants not constant?

G

geneing

How come this is illegal?

const int N = 10;
double a[ N ];

I get "error: variable-size type declared outside of any function".
What's the reason behind this syntax rule?
 
P

Peter Nilsson

How come this is illegal?

const int N = 10;
double a[ N ];

I get "error: variable-size type declared outside of any
function". What's the reason behind this syntax rule?

In C, const does not mean constant, it means read only.

C99 supports variable length arrays, but only with automatic
(local to function) storage.
 
D

Daniel Rudy

How come this is illegal?

const int N = 10;
double a[ N ];

I get "error: variable-size type declared outside of any function".
What's the reason behind this syntax rule?

A const declaration creates a read-only variable. If you want a true
constant who's value is known at compile time, then do this:

#define N 10
 
E

Eric Sosman

How come this is illegal?

const int N = 10;
double a[ N ];

I get "error: variable-size type declared outside of any function".
What's the reason behind this syntax rule?

There is no syntax problem here; no "syntax rule" is violated.
This is a semantic violation, namely, the syntactically correct but
non-constant `N' is used in a context where a constant expression
is required. (As others have explained, `const' is only 62.5%
`constant'.)
 
G

geneing

Daniel said:
A const declaration creates a read-only variable. If you want a true
constant who's value is known at compile time, then do this:

#define N 10

Wasn't the point of having "const" (in part) to be able to replace
#define?
 
E

E. Robert Tisdale

How come this is illegal?

const int N = 10;
double a[N];

I get "error: variable-size type declared outside of any function".
What's the reason behind this syntax rule?

This is a flaw in the design of the C computer programming language
which was fixed in the C++ computer programming language
but could not be fixed in C
because so much legacy code depended upon it.
 
P

Peter Nilsson

E. Robert Tisdale said:
How come this is illegal?

const int N = 10;
double a[N];

I get "error: variable-size type declared outside of any
function". What's the reason behind this syntax rule?

This is a flaw in the design of the C computer programming
language which was fixed in the C++ computer programming
language but could not be fixed in C because so much legacy
code depended upon it.

C took the const keyword from C++. Thus, C++ did not 'fix'
existing C usage of const, and there was no 'legacy code'
to speak of.
 
D

Daniel Rudy

At about the time of 5/10/2005 6:50 PM, (e-mail address removed) stated the
following:
Wasn't the point of having "const" (in part) to be able to replace
#define?

AFAIK, that applies only in C++. In C, you still have to use #define
for true compile time constants.
 
L

Lawrence Kirby

How come this is illegal?

const int N = 10;
double a[N];

I get "error: variable-size type declared outside of any function".
What's the reason behind this syntax rule?

This is a flaw in the design of the C computer programming language
which was fixed in the C++ computer programming language
but could not be fixed in C
because so much legacy code depended upon it.

How would "fixing" this in C break legacy code?

Lawrence
 

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

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top