Joana Rigbi Joana Rigbi - 19 days ago 6
C Question

IAR Expression expression expcted

Programming with IAR EWB. It gave me an error when I tried resetting the array[20]. Task was to record data based on an event. When the event happens, I recorded 6 bytes out of those 20. Next time when the event happens, I recorded next 6 bytes of those 20 and then next 6 bytes. Remaining 2 bytes I ignore.

I declared global array[20]. When the event happens 3 times, I wanted to reset array[20]. One way of doing that is array[20] = {0}. But when I did that it gave me an error saying expression expected. Note that No Macros are involved here.

So I tried using for loop and making each element of array 0 and it worked. I dont understand why. Any thoughts?

Answer

The syntax you're describing:

typename array[20] = {0}

can only be used in an initializer, where array[20] is a declaration of the array, not a reference to a specific element.

Trying to use this syntax in a statement, e.g.

array[20] = {0}

reads as if you're trying to set the single element array[20] (which is off the end of the array) to {0}, which isn't an appropriate value for it.

Use a loop.