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
  • hi,

    your problem is that you try to use the value of program variable as a number for the compiler process. See, what does next line do:

    const unsigned char code BUF_SIZE = 40;
    it says to compiler that the user needs with one byte in code memory which must be filled with number 40 in. Then this byte becomes the variable labeled "BUF_SIZE". Okay, it is not a problem for compiler. Now look at the next line:
    BYTE Qbuf[BUF_SIZE];
    By this line you say to compiler: allocate memory array which size is defined by variable BUF_SIZE. But compiler does not know how to read variables! They may be read/wrote only by program, at time when your code executes. The compiler itself just know about this variable but it cannot read its value. That's why it produces error message.

    If you really need with constant data as well as with that array, so I may recommend you such way:
    #define BUF_SIZE 40
    const unsigned char code buf_size = BUF_SIZE;
    BYTE Qbuf[BUF_SIZE];
    

    Regards,
    Oleg

Reply
  • hi,

    your problem is that you try to use the value of program variable as a number for the compiler process. See, what does next line do:

    const unsigned char code BUF_SIZE = 40;
    it says to compiler that the user needs with one byte in code memory which must be filled with number 40 in. Then this byte becomes the variable labeled "BUF_SIZE". Okay, it is not a problem for compiler. Now look at the next line:
    BYTE Qbuf[BUF_SIZE];
    By this line you say to compiler: allocate memory array which size is defined by variable BUF_SIZE. But compiler does not know how to read variables! They may be read/wrote only by program, at time when your code executes. The compiler itself just know about this variable but it cannot read its value. That's why it produces error message.

    If you really need with constant data as well as with that array, so I may recommend you such way:
    #define BUF_SIZE 40
    const unsigned char code buf_size = BUF_SIZE;
    BYTE Qbuf[BUF_SIZE];
    

    Regards,
    Oleg

Children
No data