user39997: I'm defining a variable as integer and want to use thati
int specLength = 17;
int spec[specLength-1];

When I do this, I get the following error message:
 TestProgram:34: error: array bound is not an integer constant before  ']' token
 int spec[specLength-1];

But when I set it up as 
int specLength = 17;
int spec[16];

it all works fine.
What is the magic here?
Many thanks

user-1: You have to use a constant expression, one that produce always the same value.
const int specLength = 17;
int spec[specLength - 1];

Otherwise, the compiler won't know how much space reserve for the array.
The problem with C/C++ is that the compiler doesn't really know if a variable is modified somewhere or not (thru pointers, for example). You have to tell the compiler that something is static, not changing. 
You can also use a #define for the same porpuse:
#define SPEC_LENGTH 17
int spec[SPEC_LENGTH]

Why both? const int val=17 means that val has a type (int). Using #define is just a text search-and-replace before compiling. So, SPEC_LENGTH has no type at all. If needed, you have to use a cast, like (int) SPEC_LENGTH.

39997: <p>I'm defining a variable as integer and want to use thati</p>

<pre><code>int specLength = 17;
int spec[specLength-1];
</code></pre>

<p>When I do this, I get the following error message:</p>

<pre><code> TestProgram:34: error: array bound is not an integer constant before  ']' token
 int spec[specLength-1];
</code></pre>

<p>But when I set it up as </p>

<pre><code>int specLength = 17;
int spec[16];
</code></pre>

<p>it all works fine.</p>

<p>What is the magic here?</p>

<p>Many thanks</p>
 -1: <p>You have to use a constant expression, one that produce always the same value.</p>

<pre><code>const int specLength = 17;
int spec[specLength - 1];
</code></pre>

<p>Otherwise, the compiler won't know how much space reserve for the array.</p>

<p>The problem with C/C++ is that the compiler doesn't really know if a variable is modified somewhere or not (thru pointers, for example). You have to tell the compiler that something is <code>static</code>, not changing. </p>

<p>You can also use a #define for the same porpuse:</p>

<pre><code>#define SPEC_LENGTH 17
int spec[SPEC_LENGTH]
</code></pre>

<p>Why both? <code>const int val=17</code> means that <code>val</code> has a type (<code>int</code>). Using <code>#define</code> is just a text search-and-replace before compiling. So, SPEC_LENGTH has no type at all. If needed, you have to use a cast, like <code>(int) SPEC_LENGTH</code>.</p>
