0

In AVR programming with atmega32, I can't assign a value to an array. I am getting the error:

Assignment of read-only str[i]

What am I doing wrong?

My code is:

const char str[1000];
void Serial_tx(unsigned char ch)
{
  for (i = 0; i < 10; i++)
  {
    str[i] = ch;
  }
}
Daniel Hedberg
  • 5,677
  • 4
  • 36
  • 61

2 Answers2

3

The array is declared const, indicating it should not be modified. On a microcontroller this is even more meaningful as const variables may be stored in (effectively) read-only memory (such as Flash, EEPROM, or ROM).

jerry
  • 2,581
  • 1
  • 21
  • 32
0

totally agree with jerry ...

just need to add if you need the array as const then it should be declared/defined like this:

const char str[11]={'0','1','2','3','4','5','6','7','8','9',0 };

- but tis means that you can only read str[] on runtime !!!

if you want to change the content of str on runtime than it can not be const:

char str[1000]={0};

- this allows you to read/write access on runtime

beware that total size of your non const variables, stack and C/C++ language engine cannot exceed the target device RAM memory !!!
If it does then compiler usually throws some error... but not always (sometimes stack is not fully accounted for)

Spektre
  • 49,595
  • 11
  • 110
  • 380