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
  • Your problem is that your "constant" isn't actually a constant. It's a variable flagged "const", which is a rather different thing.

    The only other major alternative to #define to create an actual constant (what the C standard calls a "compile-time constant expression") is to make it an enum label, i.e.

    enum {BUF_SIZE = 40};

    Note that this is a generic C issue unrelated to Keil tools, so this is not really the right place to discuss it.

Reply
  • Your problem is that your "constant" isn't actually a constant. It's a variable flagged "const", which is a rather different thing.

    The only other major alternative to #define to create an actual constant (what the C standard calls a "compile-time constant expression") is to make it an enum label, i.e.

    enum {BUF_SIZE = 40};

    Note that this is a generic C issue unrelated to Keil tools, so this is not really the right place to discuss it.

Children
  • I was surprised since other compilers compiled the const version without errors (GNU X-Tools for one).

    In addition, I've read Good Style practices that recommended using const rather than #define, so I wouldn't expect using it to violate ANSI C rules.

  • "I was surprised since other compilers compiled the const version without errors (GNU X-Tools for one)."

    I you sure that they were all 'C' compilers - not C++?

    Also, are you sure that you tried this in their strict ANSI mode?

    "In addition, I've read Good Style practices that recommended using const rather than #define"

    Again, are you sure they were specifically for 'C' - rather than C++, or any other language?

  • Both probably weren't strict ANSI C, but the compiler wasn't a C++ compiler nor was the style guideline for C++.

  • In addition, I've read Good Style practices that recommended using const rather than #define

    Then get back to whoever wrote them and challenge them about this aspect --- they must either be talking about programming languages similar in syntax to C, but different (Java or C++, e.g.), or they simply don't know what the heck they're talking about, and should refrain from handing out advice to others.

    The loop-hole that let GCC compile this code is that it (and the C99 standard now, too) allow variable-size arrays. But for most practical purposes, good C programming style these days is still based on C89+addenda, not C99.