0
\$\begingroup\$

I want to get values of a global array in a c file. the print screen of code is shown below: enter image description here

'data' is the global array and 'slave_buffer_rx' is another global array defined in this file to get values of 'data'.each red cross has two errors:

  1. Error[Pe094]: the size of an array must be greater than zero
  2. Error[Pe028]: expression must have a constant value

I know how to get rid of errors: make assignments in a local function. the code without errors is shown below:

enter image description here

But I do not known the exact reasons and how can IAR consider it as an array with zero length. Can anyone give me some ideas?

\$\endgroup\$
  • 5
    \$\begingroup\$ Why are yo using screendumps instead of pasting text? With your method nobody can search your question for matching text. \$\endgroup\$ – jippie May 12 '13 at 9:09
2
\$\begingroup\$

There are a few problems, take the first line for example:

__IO uint8_t Slave_Buffer_Rx[0]=data[0];

This is trying to declare a zero length array and also assign a non-constant value to it. The first [0] is specifying the length of the array as zero. What you're attempting above is a static inialization so using data[0] doesn't make much sense at that point because during program initialization the pointer value won't be known.

Because you're using pointers and the values will be one and the same I don't see an immediate reason to use any different variables. If your ISR routine needs to write data to your global buffer you could use something like the following for example:

INTERRUPT_HANDLER(I2C_IRQHandler, 19)
{
    data[0] = I2C_read();
    data[1] = I2C_read();
    data[2] = I2C_read();
}

If you really do need a seperate copy of the data (for example to modify without changing the main buffer) then what you've done in the second example is perfectly fine. You'll need to copy the contents of the buffer on each ISR call in case the data has changed between calls.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.