-2
\$\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\$
1
  • 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, 2013 at 9:09

2 Answers 2

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\$
0
-2
\$\begingroup\$

Maybe thou art to enable checkbox "Allow VLA" in the Options -> Runtime checking -> C/C++ Compiler -> Language1 -> C dialect -> StandardC -> Allow VLA in IAR Embedded Workbench 9.30.1.

"Allow VLA" means you switch on support for variable length array (VLA) in your IAR's C99 complier.

https://www.reddit.com/r/cpp_questions/comments/pfoohj/i_keep_getting_this_error_when_declaring_an_array/

"That being said, most compilers have support for variable length arrays (VLA), if you try your code here https://www.onlinegdb.com/online_c++_compiler there seems to be no problem."

\$\endgroup\$
2
  • \$\begingroup\$ That still won't resolve the problem or errors in the code so even if you enable VLAs it won't help and thus this is not a correct answer or even helpful. \$\endgroup\$
    – Justme
    Jul 5, 2023 at 4:45
  • \$\begingroup\$ Furthermore zero-length arrays is a gcc non-standard extension not supported by other compilers. Mainly because it is a feature which makes no sense and because it finally went 100% obsolete with C99 flexible array members, well over 20 years back. \$\endgroup\$
    – Lundin
    Jul 5, 2023 at 6:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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