<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://community.arm.com/utility/feedstylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Compiler error when declaring array using constant</title><link>https://community.arm.com/developer/tools-software/tools/f/keil-forum/17672/compiler-error-when-declaring-array-using-constant</link><description> 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</description><dc:language>en-US</dc:language><generator>Telligent Community 10</generator><item><title>RE: Compiler error when declaring array using constant</title><link>https://community.arm.com/thread/96368?ContentTypeID=1</link><pubDate>Fri, 11 Jun 2004 07:26:02 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:601ede98-638a-4b1b-921a-bea8def8cd63</guid><dc:creator>HansBernhard Broeker</dc:creator><description>&lt;p&gt;&lt;i&gt;In addition, I&amp;#39;ve read Good Style practices that recommended using const rather than #define&lt;/i&gt;&lt;br /&gt;
&lt;br /&gt;
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&amp;#39;t know what the heck they&amp;#39;re talking about, and should refrain from handing out advice to others.&lt;br /&gt;
&lt;br /&gt;
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.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Compiler error when declaring array using constant</title><link>https://community.arm.com/thread/42336?ContentTypeID=1</link><pubDate>Thu, 10 Jun 2004 00:40:55 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:7c4568a8-512c-4c14-ae85-0eecb07743a3</guid><dc:creator>Oleg Sergeev</dc:creator><description>&lt;p&gt;hi,&lt;br /&gt;
&lt;br /&gt;
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:&lt;br /&gt;
&lt;pre&gt;const unsigned char code BUF_SIZE = 40;&lt;/pre&gt;
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 &amp;quot;BUF_SIZE&amp;quot;. Okay, it is not a problem for compiler. Now look at the next line:&lt;br /&gt;
&lt;pre&gt;BYTE Qbuf[BUF_SIZE];&lt;/pre&gt;
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&amp;#39;s why it produces error message.&lt;br /&gt;
&lt;br /&gt;
If you really need with constant data as well as with that array, so I may recommend you such way:&lt;br /&gt;
&lt;pre&gt;
#define BUF_SIZE 40
const unsigned char code buf_size = BUF_SIZE;
BYTE Qbuf[BUF_SIZE];
&lt;/pre&gt;
&lt;br /&gt;
Regards,&lt;br /&gt;
Oleg&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Compiler error when declaring array using constant</title><link>https://community.arm.com/thread/86813?ContentTypeID=1</link><pubDate>Wed, 09 Jun 2004 19:10:12 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:25934642-270e-4195-92b6-74c8853d843a</guid><dc:creator>Jon Ward</dc:creator><description>&lt;p&gt;Here&amp;#39;s an interesting question (for those who consider const to be the same as #define).&lt;br /&gt;
&lt;br /&gt;
How many bytes are reserved for Qbuf in the following C code?&lt;br /&gt;
&lt;br /&gt;
&lt;pre&gt;
extern const unsigned char BUF_SIZE;

BYTE Qbuf[BUF_SIZE];
&lt;/pre&gt;
&lt;br /&gt;
&lt;b&gt;Jon&lt;/b&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Compiler error when declaring array using constant</title><link>https://community.arm.com/thread/42338?ContentTypeID=1</link><pubDate>Wed, 09 Jun 2004 14:45:42 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:4b24180d-4f8b-4dfa-beab-b952f17ca7bf</guid><dc:creator>Neil Kurzman</dc:creator><description>&lt;p&gt;BYTE Qbuf[BUF_SIZE];&lt;br /&gt;
&lt;br /&gt;
BUF_SIZE must be constant not a constant variable.  It must be fix at compile time.&lt;br /&gt;
the const keyword does not mean it can not be changed.  Therefore thier can not be a style that would suggest a const.&lt;br /&gt;
&lt;br /&gt;
further more.  Overusing of &amp;quot;Style&amp;quot; may result in extra ram / rom usage.  your program need to be organize and read able, but not a slave to fashion.  Remember just because Keil got C to fit into an 8bit Cpu does not mean you can forget it is an 8bit Cpu&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Compiler error when declaring array using constant</title><link>https://community.arm.com/thread/110917?ContentTypeID=1</link><pubDate>Wed, 09 Jun 2004 13:36:23 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:06eb66bf-423b-414f-bbe1-972c7d609db4</guid><dc:creator>Brian Szuter</dc:creator><description>&lt;p&gt;Both probably weren&amp;#39;t &lt;i&gt;strict&lt;/i&gt; ANSI C, but the compiler wasn&amp;#39;t a C++ compiler nor was the style guideline for C++.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Compiler error when declaring array using constant</title><link>https://community.arm.com/thread/96370?ContentTypeID=1</link><pubDate>Wed, 09 Jun 2004 13:35:44 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:5ecb05bd-16d9-4915-842b-866eec3ac7ba</guid><dc:creator>Andy Neil</dc:creator><description>&lt;p&gt;&lt;i&gt;&amp;quot;const in ANSI C isn&amp;#39;t really a compile-time constant&amp;quot;&lt;/i&gt;&lt;br /&gt;
&lt;br /&gt;
As is obvious from a quick look at at &lt;b&gt;string.h&lt;/b&gt;:&lt;pre&gt;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);&lt;/pre&gt;Clearly the &lt;b&gt;const&lt;/b&gt;s here can&amp;#39;t be indicating compile-time constants!&lt;br /&gt;
&lt;br /&gt;
(They simply flag items that &lt;i&gt;should&lt;/i&gt; not be modified at run-time).&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Compiler error when declaring array using constant</title><link>https://community.arm.com/thread/96365?ContentTypeID=1</link><pubDate>Wed, 09 Jun 2004 13:27:30 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:2bd5578b-b711-47b5-a72d-05f25d26da43</guid><dc:creator>Andy Neil</dc:creator><description>&lt;p&gt;&lt;i&gt;&amp;quot;I was surprised since other compilers compiled the const version without errors (GNU X-Tools for one).&amp;quot;&lt;/i&gt;&lt;br /&gt;
&lt;br /&gt;
I you sure that they were all &amp;#39;C&amp;#39; compilers - not C++?&lt;br /&gt;
&lt;br /&gt;
Also, are you sure that you tried this in their strict ANSI mode?&lt;br /&gt;
&lt;br /&gt;
&lt;i&gt;&amp;quot;In addition, I&amp;#39;ve read Good Style practices that recommended using const rather than #define&amp;quot;&lt;/i&gt;&lt;br /&gt;
&lt;br /&gt;
Again, are you sure they were specifically for &amp;#39;C&amp;#39; - rather than C++, or any other language?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Compiler error when declaring array using constant</title><link>https://community.arm.com/thread/72541?ContentTypeID=1</link><pubDate>Wed, 09 Jun 2004 12:18:09 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:2ab8a907-4140-4ed9-8d8e-d079a0e8a8d8</guid><dc:creator>Brian Szuter</dc:creator><description>&lt;p&gt;I was surprised since other compilers compiled the const version without errors  (GNU X-Tools for one).&lt;br /&gt;
&lt;br /&gt;
In addition, I&amp;#39;ve read &lt;i&gt;Good Style&lt;/i&gt; practices that recommended using const rather than #define, so I wouldn&amp;#39;t expect using it to violate ANSI C rules.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Compiler error when declaring array using constant</title><link>https://community.arm.com/thread/86814?ContentTypeID=1</link><pubDate>Wed, 09 Jun 2004 12:01:03 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:deaf62af-a4e9-4fb0-b241-21a12410309c</guid><dc:creator>Drew Davis</dc:creator><description>&lt;p&gt;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:&lt;br /&gt;
&lt;br /&gt;
&lt;pre&gt;
typedef enum
    {
    ThingOne,
    ThingTwo,

    NumThings
    } Thing;

U16 thingRefCount[NumThings];
&lt;/pre&gt;
&lt;br /&gt;
&lt;b&gt;const&lt;/b&gt; in ANSI C isn&amp;#39;t really a compile-time constant as in many languages.  It&amp;#39;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&amp;#39;s a wart; we just have to grin and bear it.&lt;br /&gt;
&lt;br /&gt;
As I recall, ANSI C also requires that actual storage be allocated for &amp;quot;consts&amp;quot;.  It&amp;#39;s usually more efficient for the code generator to inline the const as an immediate value in an instruction, but a &amp;quot;const int&amp;quot; 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&amp;#39;t require consts to have storage if they don&amp;#39;t actually need it.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Compiler error when declaring array using constant</title><link>https://community.arm.com/thread/42341?ContentTypeID=1</link><pubDate>Wed, 09 Jun 2004 11:38:39 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:6a629207-d440-46b7-84de-ccc342aa9999</guid><dc:creator>Andy Neil</dc:creator><description>&lt;p&gt;&lt;i&gt;&amp;quot;but this isn&amp;#39;t optimal for style&amp;quot;&lt;/i&gt;&lt;br /&gt;
&lt;br /&gt;
So you prefer &lt;i&gt;&amp;quot;style&amp;quot;&lt;/i&gt; over the language syntax rules, then...?  ;-)&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Compiler error when declaring array using constant</title><link>https://community.arm.com/thread/50269?ContentTypeID=1</link><pubDate>Wed, 09 Jun 2004 11:30:56 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:5097ab4a-3d31-405b-8bbd-63bd4bf0f388</guid><dc:creator>HansBernhard Broeker</dc:creator><description>&lt;p&gt;Your problem is that your &amp;quot;constant&amp;quot; isn&amp;#39;t actually a constant.  It&amp;#39;s a variable flagged &amp;quot;const&amp;quot;, which is a rather different thing.&lt;br /&gt;
&lt;br /&gt;
The only other major alternative to #define to create an actual constant (what the C standard calls a &amp;quot;compile-time constant expression&amp;quot;) is to make it an enum label, i.e.&lt;br /&gt;
&lt;br /&gt;
&lt;pre&gt;enum {BUF_SIZE = 40};&lt;/pre&gt;
&lt;br /&gt;
Note that this is a generic C issue unrelated to Keil tools, so this is not really the right place to discuss it.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>