Hi
Well,
int myBuffer[5];
is just what you want (space for 5 integers?)
The BUFFER_SIZE code you posted looks like C to me. You are doing the right thing by using
const int N=5;
instead. C didn't have consts originally, so C programmers had to use #defines to name constant values.
(The compiler uses the define to do a literal search and replace of BUFFER_SIZE, so the code which is actually compiled will be
item buffer[10];
, assuming BUFFER_SIZE didn't get modified)
But, in your first code snippet,
sizeof(myBuffer)
should be
sizeof(myBuffer)/sizeof(myBuffer[0])
(size of whole buffer divided by size of first element)
or just
5
, as you already know the size.
sizeof() gives the size of a variable or type in bytes. As myBuffer is an array of ints, the size in bytes will (assuming 4-byte ints) will be 5 * 4 = 20 bytes. So your loop is going too far.
If you use the const, like in your second fragment, you can use N for the loop condition
1 2 3 4 5 6 7 8
|
const int N=5;
int myBuffer[N];
// etc
for(i;i<N;i++){
// etc
|
Andy
PS I haven't tried to understand what you're trying to do inside your loop.