This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Compiler error when declaring array using constant

The following code generates a this error when compiled:

C51 COMPILER V6.02 - SN: XXXXX-XXXXX
COPYRIGHT KEIL ELEKTRONIK GmbH 1987 - 1999
*** ERROR C221 IN LINE 7 OF QUEUE_I.H: non-constant case/dim expression

const unsigned char code BUF_SIZE = 40;

BYTE Qbuf[BUF_SIZE];

I get the same error whether or not I use "code". I've worked around this by using
#define BUF_SIZE 40
but this isn't optimal for style, and debugging purposes.

Has anyone else seen this problem?

Parents
  • The third major alternative for declaring integer constants is to use an enum. This is of course most useful for grouping a related series of constants, rather than individual ones. But enum values can be used to size arrays:

    typedef enum
        {
        ThingOne,
        ThingTwo,
    
        NumThings
        } Thing;
    
    U16 thingRefCount[NumThings];
    

    const in ANSI C isn't really a compile-time constant as in many languages. It's a hint to the compiler that you intend not to overwrite the variable in question. The grammar does not allow a variable (even a const one) to occur as the size in an array declaration. It's a wart; we just have to grin and bear it.

    As I recall, ANSI C also requires that actual storage be allocated for "consts". It's usually more efficient for the code generator to inline the const as an immediate value in an instruction, but a "const int" will usually turn into a reference to a value in some memory location, which increases both code size and time. C++ is a little smarter, and doesn't require consts to have storage if they don't actually need it.

Reply
  • The third major alternative for declaring integer constants is to use an enum. This is of course most useful for grouping a related series of constants, rather than individual ones. But enum values can be used to size arrays:

    typedef enum
        {
        ThingOne,
        ThingTwo,
    
        NumThings
        } Thing;
    
    U16 thingRefCount[NumThings];
    

    const in ANSI C isn't really a compile-time constant as in many languages. It's a hint to the compiler that you intend not to overwrite the variable in question. The grammar does not allow a variable (even a const one) to occur as the size in an array declaration. It's a wart; we just have to grin and bear it.

    As I recall, ANSI C also requires that actual storage be allocated for "consts". It's usually more efficient for the code generator to inline the const as an immediate value in an instruction, but a "const int" will usually turn into a reference to a value in some memory location, which increases both code size and time. C++ is a little smarter, and doesn't require consts to have storage if they don't actually need it.

Children
  • "const in ANSI C isn't really a compile-time constant"

    As is obvious from a quick look at at string.h:

    extern char *strchr  (const char *s, char c);
    extern int   strpos  (const char *s, char c);
    extern char *strrchr (const char *s, char c);
    extern int   strrpos (const char *s, char c);
    Clearly the consts here can't be indicating compile-time constants!

    (They simply flag items that should not be modified at run-time).