sh***@133sh.com <sh***@133sh.com> wrote:
test1.c
------------------------------
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv)
{
const int MAX_CHAR_NUM=10;
char name[MAX_CHAR_NUM]="Computer";
printf("My name is %s.\n", name);
exit(EXIT_SUCCESS);
}
test2.c
-----------------------------
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv)
{
enum width {MAX_CHAR_NUM=10, OTHER};
char name[MAX_CHAR_NUM]="Computer";
printf("My name is %s.\n", name);
exit(EXIT_SUCCESS);
}
Test1 can not be compiled.
And test2.c can.
Why?
Although one would think that using the keyword const makes the object
a constant that is not essentially true. In an array declaration
(definition) you must use a constant that can be evaluated at compile
time, but not at run time. An enumeration value is such a constant, but
const is just qualifying the object such that it cannot (should not) be
changed directly or indirectly, but there will be an object. The value
of an object cannot be evaluated at runtime.
As a rule, if any of the operands of an expression can be pointed to
with a pointer (or the address can be evaluated) the expression cannot
be constant.
--
Z (Zo**********@daimlerchrysler.com)
"LISP is worth learning for the profound enlightenment experience
you will have when you finally get it; that experience will make you
a better programmer for the rest of your days." -- Eric S. Raymond