Possible Duplicate:
Declaring an array with a non-constant size variable
This is my code:
const int xsize=150;
char Hey[xsize];
I don't understand why I cannot declare my new array Hey using the constant above. Can anyone help in this?
Possible Duplicate:
Declaring an array with a non-constant size variable
This is my code:
const int xsize=150;
char Hey[xsize];
I don't understand why I cannot declare my new array Hey using the constant above. Can anyone help in this?
It's not valid C89 code. You can't declare an array with variable size, even if the variable happens to be const
.
It would work if you had it as a #define
rather than a const int
. It is valid in C99, though. GCC and other compilers also offer it as an extension in C89 mode.
Because const
does not create a constant. It creates a read-only object.
The difference between object and constant is that objects have a specific memory location they live at; constants live only in source code.
In C89, an array size should be a constant expression. A const
variable is not a constant expression. To do this, you have to use rather VLA, from C99.